Standard file operation function (reproduced)

xiaoxiao2021-04-03  232

The document read and write functions described in this section refers to sequential reading and writing, that is, after reading a message, the pointer is automatically added 1. The write operation function and the read operation function are described below.

1. The order of files of files fprintf (), fputs (), and fputc () function FPRINTF (), fputs (), and fputc () are the order write operation function of the file, which call the format as follows: int FPRINTF (file * Stream, Char * Format, ); int FPUTS (Char * String, File * Steam); int FPUTC (INT Ch, File * Steam); the return value of the above three functions is an integer amount. The return value of the fprintf () function is the number of words (bytes) that actually writes in the file. If you write an error, return a negative number, and the fputs () function returns 0 indicates that the operation in the string of the string pointer to the file is successful, and when it is not 0, it indicates that the write operation failed. The FPUTC () function returns a value of the characters written to the file. At this time, the write operation is successful, otherwise it returns EOF (the end of the file ends is -1, defined in stdio.h) means a write operation error. The formatted specification in the fprintf () function is the same as the Printf () function, and the difference is just the fprintf () function is written to the file. PRINTF () is output to the screen. Here, an example is introduced, the file after running the post-print Test.dat. Example 11: #include main () {char * s = "That's good news"); / * Define string pointer and initialize * / int i = 617; / * Define integer variables and initialize * / File * fp; / * Define file pointer * / fp = fopne ("test.dat", "w"); / * Create a text file only * / FPUTS ("Your score of toeflis", fp); / * Write a string of characters * / fputc (':', fp) to the files; / * Write the colon to the file: * / fprintf (fp, "% d / n", i); / * to the built The file writes an integer * / fprintf (FP, "% s", s); / * Write a string * / fclose (fp) to the filed file; / * Close the file * /} Show with the Type command The contents of Test.dat are as follows: Screen Shows Your Scorefl IS: 617 That's Good News

2. Document sequential read operation function fscanf (), fgets (), and fgetc () function FSCANF (), fgets (), and fgetc () are the order read operation function of the file, and its call format is as follows: int Fscanf (file * stream, char * format, ); char fgets (char * string, int n, file * steam); int FGETC (File * Steam); the usage of fscanf () functions is similar to the scanf () function, Just it is read from the file. The return value of the fscanf () function is EOF (ie -1), indicating that the read error is read, otherwise the read data is successful. The FGETS () function reads up to N-1 characters from the file (n to specify the number of characters), and put them in the string pointing to the string, automatically add an empty character to the string after reading. , Read successfully returns a String pointer, and fails to return an empty pointer. The fgetc () function returns a character in the current location, return EOF when reading the error. The following program reads the Test.dat file generated by Example 11 and display the result of the readout on the screen. Example 12 #include main () {char * s, m [20]; int I; file * fp; fp = fopen ("test.dat", "r"); / * Open text file only Read * / fgets (S, 24, FP); / * Read 23 characters * / Printf ("% s", s) from the file; / * Output The read string * / fscanf (fp, "%) D ", & i); / * Read the integer * / printf ("% d ", i); / * Output the integer * / PUTCHAR (FGETC (FP)); / * Read a character Output * / fgets (m, 17, fp); / * Read 16 characters * / PUTS (M); / * Output The read string * / fclose (fp); / * Close the file * / getch (); / * Wait a button * /} Running screen display: Your score of toefl IS: 617 That's good news If fscanf (fp, "% D", & i) is changed to FSCANF (FP, "% s", m , Then change the subsequent output statements to Printf ("% s", m), and can be obtained. This can be seen in Turbo C2. 0, as long as it is a text file, whether it is character or a number will be processed according to its ASCII value. It is also necessary to explain that when the fscanf () function is read, it is automatically ended, and it should be paid to it when used.

3. Random reading and writing of files Sometimes users want to directly read the information somewhere in the middle of the file, if reading and writing if the order of files must start from the file head until the required file position is read, which is obviously inconvenient. Turbo C2.0 provides a set of files for a set of files, you can locate file position pointer directly to read and write. The random read and write function of the file is as follows: int FSeek (file * stream, long offset, int.comwhere); int 4, file * stream); int FWRITE (Void * buf, int size, INT Count, file * stream; long ftell (file * stream); FSeek () function The role of FSeek () is to set the location pointer of the file to the origin of the originth byte starting from the fromwhere, where fromwhere is the following macro definitions. One: File Location Pointer Starting Calculation Location fromWhere ━━━━━━━━━━━━━━━ 符 符 常━ 数 数 数 数 含─ 常 常 - ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── ────────────────- Seek_set 0 From the beginning of the file, SEEK_CUR 1 SEEK_END 2 from the file pointer SEEK_END 2 from the end of the file, ━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━ ━ ━ 指 文件 文件 位置 位置 数 数 数 节 节 节 节 节 节 节 字 节 节 节 字 字.. 数......... It is a long amount to support a file greater than 64K bytes. The fseek () function is generally used to operate the binaries. When the FSeek () function returns 0, it indicates that the operation is successful, and the return non-0 is failed. The following program reads the 8th byte from the binary test_b.dat. Example 13: #include main () {file * fp; if ((fp = fopen ("Test_b.dat", "RB")) == null) {Printf ("can't open file" EXIT (1);} fseek (fp, 8.1, seek_set); fgetc (fp); fclose (fp);} The FREAD () function is a field from the file in the file, each field length is SIZE Bytes and store them into buffers referred to in the BUF pointer. The fwrite () function is written in a file from the COUNT field referred to by the BUF pointer to the COUNT field of the SIZE byte to the stream pointing. With the increase of the reading and writing, the file location indicator also increases, how many bytes are read, and how many bytes are skipped accordingly. The read and write function returns the number of fields read and written. The ftell () function returns the current value of the file location indicator. This value is the number of bytes that the indicator starts from the file header. The number returned is long. When -1 is returned, it indicates an error. The following program writes a floating point array in a binary manner in the file test_b.dat.

Example 14: #include main () {float f [6] = {3.2, -4.34, 25.04, 0.1, 50.56, 80.5}; / * Define floating point arrays and initialize * / int i; file * fp; fp = fopen ("test_b.dat", "wb"); / * Create a binary only * / fwrite (f, sizeof (float), 6, fp); / * Write 6 floats In the file * / fclose (fp); / * Close the file * /} The following example reads 100 integers from the Test_b.dat file and puts them in the DAT array. Example 15: #include main () {file * fp; int Dat [100]; fp = fopen ("test_b.dat", "rb"); / * Open a binary read-only * / if (Fread (DAT, SIZEOF (INT), 100, FP)! = 100) / * Judging whether 100 numbers * / {IF (FeOf (FP)) Printf ("end of file"); / * is not 100 files end * / else printf ("read error"); / * reading error * / fclose (fp); / * Close file * /} Note: When using the standard file function to read and write the file, first Put the contents of the read-written content into the buffer, that is, the write function is only operated on the output buffer, and the read function is only operated on the input buffer. For example, write content to a file, the content written will be first placed in the output buffer until the output buffer is full or using the Fclose () function to turn off the file, the contents of the buffer are written to the file. If there is no fclose () function, the content incomplete content is not stored in the file or written to the file. There is a function of refreshing the buffer, which is fflush (), which calls the format: int FRUSH (file * stream); this function will actually write the content of the output buffer, and clear the contents of the input buffer Drop. 4. Feof () and rewind () functions These two functions are: int fal *; int rebind (file * stream); feof () function detects whether the file location indicator arrives at the end of the file, If it returns a non-0 value, it returns 0. This function is especially useful to binary file operations, because in binary files, file end flag EOF is also a legitimate number of binary numbers, only to check the value of reading characters to determine if the file is endless. If so, the file may be not ended without ending, and you must have a Feof () function. The following statement is a method that the common judgment file is ended. While (! Feof (fp)) FGetc (fp); while is a loop statement, will be described below. The REWIND () function is used to move the file location indicator to the starting point of the file, return 0 when it is successful, otherwise, return non-0 value

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

New Post(0)