It is found that file operations are always required for recently written programs, so review the functions related to the C language and file input and output.
C language is used to associate a file type pointer with a file to open, close, input, and output the file.
The FILE type is FILE (actually a struct ). Define a FILE pointer as FILE * fp. Then you can associate fp with a FILE to perform operations. For example, to open a file:
FILE * fp;
Fp = fopen ("filename", "open mode"); // the return value of fopen is NULL or a pointer to the file
Or directly FILE * fp = ("filename", "open mode ");
To redirect, you only need to associate the fp pointer with another file. This means that multiple files can be operated using one file pointer.
Close the file after use. This is a good habit. The close function is fclose (fp). In this way, fp is not associated with the file pointed to previously.
1. Determine whether the file is successfully opened: www.2cto.com
Whether the file is successfully opened or not. if (fp = fopen ("filename", "r") = NULL), the file fails to be opened.
2. Determine whether the file is ended:
To determine whether the ASCII file ends, fgetc () will return the end mark EOF (-1). Because the binary file data will show-1, you must use the feof (fp) function to determine whether the end mark is "-1", and feof (fp) = 1.
3. input and output of a single character: fgetc () and fputc ()
Ch = fgetc (fp); fp is the file pointer, and fgetc () does not ignore spaces and carriage return characters, and can only be used to read characters.
The corresponding fputc (ch, fp). When outputting characters to a file, spaces are not ignored.
Because fgetc () returns characters, fgetc () and fputc () are usually nested using: fputc (fgetc (in), out ).
Fputc () returns the input character or EOF (-1) (in case of failure)
The returned value of fgetc () is the read character (including the end mark EOF (-1 ))
4. Input and Output of data blocks: fread (buff, size, count, fp) and fwrite (buffer, size, count, fp)
Buffer is a pointer. For fread, It is the storage address for reading data, and for sending fwrite, It is the address for outputting data,
Size refers to the number of bytes to be read and written, and count refers to the number of size bytes to be read and written.
If both successful fread and fwrite return successful input and read data blocks, the number of data blocks may be smaller than that of count;
Note: The buffer parameter of fread must point to a space greater than or equal to the size of the data block to be read.
Fread and fwrite are generally used for input and output of binary files. The conversion of ASCII characters may be different from the original assumption.
5. putw () and getw () are integers used to read and write disk files.
For example, putw (10, fp); // the return value is the output number (int)
Int I = getw (fp); // if it fails,-1 is returned.
Note that both putw () and getw () are output in binary format.
So if you use putw () to input data to a file and open it in text mode, you will see garbled characters.
Similarly, if you input a number in a text file and save it, you can use getw () to read it. Because it is read in binary format.
From shimachao's column