C language programming study notes-input and output of files,
C language programming learning notes-input and output of files.
10.1 basic knowledge about C files
1. Definition:
File: a collection of data (data files) stored on external media. The operating system manages data in files.
2. Category:
From the user's point of view:
? Special files: standard input/output files or standard device files.
? Common files: disk files.
2. From the Operating System Perspective: each input/output device associated with the host can be considered as a file.
(1) objects are organized into sequential access and random access.
(2) files are stored in ASCII and binary formats.
3. File Buffer
In C, file access is measured in characters (bytes). An input/output stream is a byte stream or binary stream.
Files are stored in a buffer file system or a non-buffer file system. The difference is that the buffer file system automatically opens the buffer, and the non-buffer file system sets the buffer for each file by the program.
Ansi c only uses a buffer file system to process files.
4. file type struct type
In the buffer FILE system, each used FILE opens a FILE struct type area in the memory, used to store information about a file (such as the file name, File status, current location, and buffer ),
FILE struct type prototype:
? Typedef struct {
Short level;/* the degree to which the buffer is "full" or "empty */
Unsigned flags;/* File status flag */
Char fd;/* file descriptor */
Unsigned char hold;/* do not read characters without a buffer */
Short bsize;/* buffer size */
Unsigned char * buffer;/* Data buffer location */
Unsigned char * curp;/* pointer, current point */
Unsigned istemp;/* temporary file, indicator */
Short token;/* used for validity check */} FILE;
5. array and pointer of file struct
? FILE * fp -- declares a pointer variable pointing to a FILE-type struct.
? FILE f [5] -- declares a FILE structure array f, which has five elements and stores the information of five files.
? FILE variable -- declares a FILE struct variable.
10.2 open and close a file
C language requirements: "Open" the file before reading and writing the file, and "close" the file after use.
1. open the file:
? FILE * fp;
? Fp = fopen ("file name", "use file method"); pointer variable points to the opened file.
For example, fp = fopen ("a1", "r ").
2. close the file:
Fclose (File pointer); Return Value: 0 if close is successful; otherwise, EOF (-1) is returned ).
So that the file pointer variable does not point to the file, that is, the file pointer variable is "decoupled" from the file, and then the pointer can no longer be used to read or write the file associated with it.
10.3 read and write data files in sequence
1. Read and Write characters to a file
(1) write characters to the file:
Fputc (ch, fp); Return Value: If the output is successful, the return value is the output character; If the output fails, an EOF (-1) is returned ).
Output the character (ch value) to the file pointed to by the file pointer fp. The file must be opened in write or read/write mode.
(2) read the characters from the file:
Ch = fgetc (fp); Return Value: a character that is successfully read and assigned to ch. If a file Terminator is encountered, a file Terminator EOF (-1) is returned ).
Read a character from a specified file. The file must be opened in read or read/write mode.
Note: ansi c provides a feof () function to determine whether the file ends. If the file ends, the value of the feof (fp) function is 1 (true); otherwise, it is 0 (false ). The preceding steps also apply to reading text files.
2. Read and Write a string to a file
? Fgets (str, n, fp); read n-1 characters from the file pointed to by fp, and add '\ 0' at the end '. Return Value: the first address of str. If EOF is encountered, the read ends.
? Fputs ("china", fp); writes a string to the file pointed to by fp. The first parameter can be a String constant, character array name, or character type pointer. '\ 0' at the end of the string is not output.
3. Read and Write files in formatting Mode
? Fprintf (File pointer, Format String, output table column );
? Fscanf (File pointer, Format String, input table column );
Note:
The fprintf and fscanf functions are used to read and write disk files. They are easy to use and understand. However, the ASCII code must be converted to binary format during input, it takes a lot of time to convert the binary format into characters during output. Therefore, when the memory and disk frequently exchange data, it is best to use fread and fwrite functions instead of fprintf and fscanf functions.
4. Data Block read/write:
? Fread (buffer, size, count, fp );
? Fwrite (buffer, size, count, fp );
Buffer: a pointer. For fread, It is the storage address for reading data. For fwrite, it refers to the address (both the starting address) of the data to be output ).
Size: the number of bytes to read and write.
Count: the number of size-byte data items to be read and written.
Fp: file pointer.
5. Read and Write Functions in the unit of "word" or INTEGER:
? Putw (int I, FILE * fp );
? Int I = getw (FILE * fp );
10.4 random read/write data files
Can Random Access access data at any location?
1. Mark and locate the file location
File Location MARK: used to indicate the location of the next character to be read and written ".
2. Positioning of the file location Tag: you can force the file location tag to point to the location specified by people.
1. Return the current position pointer of the file to the starting position of the file: no return value.
3. A. rewind (fp); after the command is executed, the position pointer of the file is located at the beginning of the file.
Random read/write: changes the position pointer of a file, which is generally used for binary files.
Fseek (file type pointer, displacement, starting point); no return value.
Start Point: SEEK_SET 0 at the beginning of the file
Current File Location SEEK_CUR 1
SEEK_END 2 at the end of the file
C. Displacement: the number of bytes that are moved backward (before, at the end) based on the starting point. It is generally long type.
D. Get the current position pointer of the stream file: return the current position, which is expressed by the displacement relative to the beginning of the file ., EOF is returned when an error occurs.
I = ftell (fp );
10.6 file read/write error detection
1. ferror (fp); Return 0, indicating no error; return non-0, indicating an error.
Note: Check the value of the ferror function immediately after calling an input/output function. Otherwise, the information will be lost. When the fopen function is executed, the initial value of the ferror function is automatically set to 0.
2. clearerr (fp); set the file error mark and end mark to 0.
If an error flag is displayed, it is retained until the clearerr function, rewind function, or any other input/output function is called for the same file. After the call, the value of feeror (fp) is changed to 0, and the end mark of the file is set to 0.
10.6 Summary
CATEGORY function name Function
Open File fopen () Open File
Close file fclose () Close file
File Location fseek () change the file location pointer location
Rewind () causes the file location pointer to start with the file
Ftell () returns the current value of the file location pointer
If the file status feof () reaches the end of the file, the function value is true.
Ferror () if an error occurs in file operations, the function value is true.
Clearerr () sets the ferror and feof () function values to zero.
Read/write fgetc (), getc () Get a character from the specified file
Fputc () and putc () output characters to the specified file
Fgets () reads a string from a specified file
Fputs () outputs the string to the specified file
Getw () reads a word from a specified file (int type)
Putw () Outputs a word to a specified file
Fread () reads data items from a specified file
Fwrite () writes data items to a specified file
Fscanf () input data from a specified file in the format
Fprintf () writes data to a specified file in the specified format