C language random access to files

xiaoxiao2021-03-06  45

C language random access to files

C language To implement random access to files, two functions fseek (), ftell () are required. Below the programs that reverse display the specified files to introduce the use of these two functions.

RESERVE.C:

#include

#include

#define cntl_z '/ 032' // DOS text file end tag

#define Slen 50

Int main (int Argc, char * argv [])

{

Char file [slen];

CHAR CH;

File * fp;

Long count, Last;

PUTS ("Enter the name of the file to be processed:");

Gets (file);

IF ((fp = fopen (file, "rb")) == null) // read-only and binary mode

{

Printf ("Reverse CAN't open% S / N", File);

Exit (1);

}

FSeek (fp, 0l, seek_set); // Positioning at the beginning of the file

Last = FTELL (FP);

Printf ("FSEEK (FP, 0L, Seek_set), FTEEL (P):% D / N", Last);

FSeek (fp, 0l, seek_ek_ek); // Located at the end of the file

Last = FTELL (FP);

Printf ("FSeek (FP, 0L, Seek_ek_ely), FTEEL (P):% D / N", LAST);

For (count = 1L; count <= last; count )

{

FSeek (fp, -count, seek_ek_ek);

CH = Getc (FP);

IF (ch! = cntl_z && ch! = '/ r')

{

PUTCHAR (CH);

}

}

PUTCHAR ('/ n');

Fclose (fp);

System ("pause");

Return 0;

}

Assume that a file Test.txt content is:

1234567890

1234567890

1234567890

1111111112

2222222223

3333333334

Perform reserve to reverse display:

ENTER The name of the file to be processed:

Test.txt

FSEEK (FP, 0L, Seek_set), Fteel (p): 0

FSeek (fp, 0l, seek_ek, fteel (p): 72

4333333333

3222222222

2111111111

0987654321

0987654321

0987654321

Below, let's explain how fseek () and ftell () work.

l fseek () function

FSEEK (reading and writing location of mobile file stream)

related functions

Rewind, Ftell, Fgetpos, FSETPOS, LSEEK

Header file

#include

Definition function

Int fseek (file * stream, long offset, int imce);

Function description

FSeek () used to move the read and write location of the file stream. The parameter stream is the opened file pointer, and the parameter OFFSET is the number of displacements that move the read-write position based on the parameter imines. parameter

The use is one of the following: Seek_set from the beginning OFFSET displacement of the file as a new read and write location. Seek_cur increases OFFSET unloading after the current read and write position. Seek_end points the read-write position to the end of the file and increases OFFSET. When the WHENCE value is seek_cur or seek_ek_ek_ek_end, the parameter OFFSET allows the emergence of negative values. The following is a more special way: 1) When you move the read / write position to the beginning of the file: FSeek (File * Stream, 0, Seek_set); 2) When you move the read / write location to the file: FSeek (File * stream 0, 0seek_end);

return value

When the call is successful, it returns 0. If there is an error, return -1, Errno will save the error code.

Additional information

FSeek () is unlike LSeek () will return a read-write location, so you must use ftell () to achieve the current read and write location.

l ftell () function

Ftell (acquired the reading location of the file stream)

related functions

FSeek, Rewind, Fgetpos, FSETPOS

Header file

#include

Definition function

Long Ftell (File * Stream);

Function description

ftell () is used to obtain the current read and write location of the file stream. The parameter stream is the open file pointer.

return value

When the call is successful, return to the current read and write position. If there is an error, return -1, errno will save the error code.

error code

Ebadf parameter stream is invalid or removable of file streams for read-write locations.

example

Refer to FSeek ().

Through FSeek (), fTell () two functions, we can access any position of the file at will, think about it seems to be used as an operation file, so there is no more to say. Yes, FSeek () and ftell () There is a potential question to limit the size of the file only within the long-term representation of the long, that is, in this way, only open 2,000,000 bytes of files, but In most cases, it seems to have been enough. If you need to open a larger file, you need to use the fgetpos (), the fsetpos () function, that is another proposition.

转载请注明原文地址:https://www.9cbs.com/read-71527.html

New Post(0)