Document operation has the following content:
1. Output data from memory to the file, also known as "Write" file.
2. Enter the data from the file to the memory, also known as "read" file.
1. How to perform file operations:
Set up file pointer
When performing a file operation, you must first define the file pointer before starting the program. File * Pointer Type Variable 2. Open file
Connect the file pointer to the file on the disk, and all the operations of the file will make FOpen through the file pointer (file name, use file mode)
For example: fp = fopen ("a.txt", "r");
Where a.txt means a file that will be accessed is a.txt called A.txt. The FOPEN () function call is successful, indicating that the file A.txt is found in the specified disk location, and will bring back the pointer to the A.Txt file and assign it to the FP, so that FP has established contact with file A.txt. If the file is open, the fopen () function will return an empty pointer null, in order to avoid using an empty pointer, you should output failed information in writing form below: if (fp = fopen ("a.txt", "R ") == null) {Printf (" can't Find this file! / N "); exit (0);} ---------------------- -------------------------------------------------- ------------------------- File usage
"R" (read-only) opens a text file for input. If the specified file does not exist, it will be wrong.
"W" (written only) opens a text file for the output. If the file does not exist, the system will establish a new file with the specified name; if the file has been saved, it will be rewritten from the starting position, the original content is updated.
"RB" (read-only) opens a binary file for input. Function with "R"
"WB" (written) opens a binary file for the output. Function with "W"
"A" (append) opens a text file for additional data. If the file does not exist, the system will establish a new file with the specified name; if the file has been existed, the new data is written after the original content.
"AB" (additional) opens a binary file for additional data. The rest of the function is the same as "A"
"R " (read / write) opens an existing text file for read / write. Ready, written, read and write always starting from the starting position of the file; do not close the file when replacing the read and write operation
"RB " (read / write) opens an existing binary file for reading / writing. The function is the same as "R ". The starting position of the read-write can be set by the position function
"W " (read / write) creates a new text file for reading / writing. If the file already exists, the original content will be updated.
"WB " (read / write) is a new binary file for reading / writing. The function is the same as "W "; the location function can be set by the position function.
"A " (read and write) opens a text file for read / write. The function is the same as "A", but after adding new data to the end of the file, you can start reading from the beginning.
"AB " (read / write) opens a binary file for read / write. The function is the same as "A ", but after adding new data to the end of the file, you can set up the starting position of the position function setting ---------------------- -------------------------------------------------- -----------------------
Second, write data:
The read and write data will be completed by calling the standard input output function. These functions mainly include: (1) FGETC () and getc (): read a character fputc () and PUTC () from a formulated file, output a character to the specified file (2) fgets (): from the specified file A string fputs (): Output a string to the specified file (3): once in the specified file (): Output a data block to the specified file (4 Fscanf (): Read data fprintf () from the specified file: Press the data to the specified file in the specified format.
File positioning function, make "read-write position pointer" direct point to the specified read and write location: fseek (): The movable position pointer is ftell (): You can get the position of the current position pointer REWIND (): "Rexide" Function, make the position pointer back to the beginning of the file
3. Turn off the file
When reading, after the write operation is complete, you must first turn the file first.
Fclose (file pointer)
The function call is successfully returned to 0, otherwise returns a non-0 value
-------------------------------------------------- -------------------------------------------------- -----------------------------------------
How to write a single character to a text file: #include
-------------------------------------------------- -------------------------------------------------- ------------------------------------------
5. Write the text file #include
4. Read a single character from the text file #include
-------------------------------------------------- -------------------------------------------------- ------------------------------------------
File function function name function prototype Description Function Return Value ---------------------------------------- -------------------------------------------------- --- Fopen file * fopen (CHAR opens the name in mode) Successfully returns the file pointer (file * filename, the beginning address of file information for filename), otherwise char * mode); return null
example:
-------------------------------------------------- ------------------------------------------ Fclose Int Fcolse (file * fp ); Turn off the file referred to in the FP, release the file buffer error Returns 0, otherwise returns 0
example:
-------------------------------------------------- --------------------------------------------
-------------------------------------------------- -------------------------------------------------- ------------------------------------------ formatted files #include
Void Num (File * fp) {char name [20]; int Num; scanf ("% s% d", name, & num); while (nu> o) {fprintf (fp, "% s% d / n" , Name, Num); Scanf ("% S% D", Name, & num);
}
-------------------------------------------------- -------------------------------------------------- ------------------------------------------
Format reading file #include
Void Num (File * f) {char name [20]; int Nu; fscanf (f, "% s% d", name, & nu); while (! feOf (f)) {printf ("% s% d / n ", name, nu); fscanf (f,"% s% d ", name, & d);}}
-------------------------------------------------- -------------------------------------------------- ------------------------------------------
Document data block reading and writing mode Press the data block to read and write an example of a binary file: / * Data block read and write by Searcher2002-9-12 * /
#include
Main () {struct student PES [3] = {{"WANGHAI", 1015, 'M, 1985, 2, 22, 80, 95, 60.5}, {"Haixin", 1017,' f ', 1984, 1 , 2, 57, 60, 78}, {"Lili", 1019, 'F', 1984, 3, 2, 77.0, 90, 77.5}}; INT I; File * FPR; Struct Student B; FPR = FOPEN "W.dat", "wb "); for (i = 0; i <3; i ) FWRITE (PES I, SIZEOF (STRUCT), 1, FPR); FSEEK (FPR, 0L, Seek_set); for (i = 0; i <3; I ) {FREAD (& B, Sizeof (Struct Student), 1, FPR); Printf ("% 7s", B.NAME); Printf ("% C", B.SEX) PRINTF ("% D% D% 2D", B.BIRTHDAY.YEAR, B.BIRTHDAY.MONTH, B.BIRTHDAY.DAY; Printf ("% 5.1f% 5.1f% 5.1f", b.cj [0 ], B.CJ [1], B.CJ [2]); Printf ("/ n");} getchar (); fclose (fpl);} // fWrite () and FREAD () are generally used for binary files In // Fread (& B, Struct Student), 1, FPR), the first parameter represents B's address, the second parameter represents the number of bytes of data blocks read again, the third parameter Is a number 1, indicating that each time you read a block, the last one is a file pointer
-------------------------------------------------- -------------------------------------------------- -------------------------------------------
File Copy Method / * File Copy By Searcher2002-9-12 * /
#include
Void main (int Argc, char * argv []) {file * fpin, * fpout; printf ("argc =% d / n", argc); if (argc <3) {printf ("cmdline outto get at Less Than 3 cmd! / N "); exit (0);} if (argc> 3) {Printf (" cmdline outto get 3 cmd! / N "); exit (0);} if (argc == 3) {IF (FPIN = FOPEN (ARGV [1], "R")) == null) {Printf ("can't open file! / n"); exit (0);} if ((fpout = fopen (argv [argv 2], "W")) == null) {Printf ("can't open file! / N"); exit (0);} filecp (fpin, fpout); fclose (fpin); fclose (fpout); } Else printf ("error! / N");} void filecp (file * fpin, file * fpout) {char ch ;CH = getc (fpin); while (! Feof (fpin) {PUTC (CH, FPOUT) CH = Getc (fpin);}}
The above program has a problem, why is the number of command line parameters argc at runtime? ? ? ? ? ? So, the results are always: "Error!"
-------------------------------------------------- -------------------------------------------------- -------------------------------------------
File Location Function 1.fseek () function: Used to move the file position pointer to the specified location, then read, write operation. The modem of functions is as follows: FSeek (PF, Offset, Origin)
PF is the file pointer, offset is a displacement amount in bytes, which is long integer; origin is the starting point, which means which position is based on which position is based, and the starting point can be represented by the identifier. Available numbers to represent.
Identifier digital represented starting point seek_set 0 file SEEK_END 2 file end seek_cur 1 file current location
For example: FP has pointed to a binary file that allows the position pointer to move 20 bytes from the beginning of the file to the file tail, the call form is as follows: FSeek (FP, 20L, Seek_set)
For example: FP has pointed to a binary file that allows the position pointer from the end of the file to the end of the 10 SIZEOF (int) function, as follows:
FSEEK (FP, -10L * Sizeof (int), seek_end)
For text files, the displacement must be 0. For example, FP has pointed to a text file that allows the position pointer to the FSEEK () function form of the file starting position, as follows: FSeek (fp, 0l, seek_set), for example: fp, to point to a text file, can move the position pointer to The form of the FSeek () function at the end of the file is as follows: fseek (fp, 0l, seek_ek_end)
2.Ftell () function: Call the fTell () function to get the number of bytes that the current position pointer relative to the file start position. When the function calls an error, -1L. For example: When a file is opened, it usually does not know the length of the file, and the number of bytes of the file can be obtained by the following block:
FSEEK (FP, 0L, Seek_ek_ek; // Move the position pointer to the end of the file t = fTell (fp); // Detecting the total byte number of the file to, for example, the Struct ST structure type data stored in a binary file However, the number of data type data blocks can be obtained by the following procedures: FSeek (FP, 0L, SEEK_END); T = FTELL (FP); n = t / sizeof (struct ST);
3.Rewind () function: Also known as the "rebound" function, enable the position pointer to the beginning of the file. The function call form is as follows: Rewind (fp); fp is the file pointer, this function does not return value.