From

xiaoxiao2021-03-06  81

Word Source VC World - C language classroom

Self-archiving, also dedicated to the same Delphi programmer as me

Functions for multiple files are available in C language: • Characters read and write functions: FGETC and FPUTC · String read and write functions: fgets and fputs • Data block read / write functions: freed and fwrite · Format read and write functions: FSCANF and FPRINF

The following is given separately. Using the above functions requires the header file stdio.h. The character read and write function FGETC and FPUTC character read and write functions are read and write functions in characters (bytes). Each time you can read or write a character from a file.

First, read character functions FGETC

The function of the FGetc function is to read a character from the specified file. The function call is: character variable = fgetc (file pointer); for example: ch = fgetc (fp); its meaning is to read one from the open file FP Characters and feed them in CH.

The following is the following description for the FGETC function: 1. In the FGETC function call, the read file must be opened or read and write.

2. The result of reading characters can also be assigned to character variables, such as FGETC (FP); but read characters cannot be saved.

3. There is a position pointer inside the file. The current read and write byte used to point to the file. When the file is opened, the pointer always points to the first byte of the file. After using the FGETC function, the position pointer will move backward one byte. Therefore, the FGETC function can be used for multiple times, read multiple characters. It should be noted that the file pointer and the location of the file inside the file are not a matter. The file pointer is pointing to the entire file, which must be defined in the program, and the value of the file pointer is unchanged as long as it is not re-assigned. The location pointer inside the file is used to indicate the current read and write location inside the file, each read, the pointer moves backward, it does not need to be defined in the program, but is automatically set by the system.

[Example 10.1] Read file E10-1.c, output on the screen. #include main () {file * fp; char ch; if ((fp = fopen ("e10_1.c", "RT")) == null) {Printf ("Cannot Open File Strike Any Key EXIT! "); getCH (); exit (1);} ch = fgetc (fp); while (ch! = EOF) {PUTCHAR (CH); ch = fgetc (fp);} fclose (fp);} The functionality of the case is to read characters one by one from the file and display it on the screen. The program defines the file pointer FP to read the text file to open the file "E10_1.c" and cause the FP to the file. If you open a file error, give a prompt and exit the program. The program 12 first reads a character, and then enter the loop. As long as the character read is not the file end flag (one end flag EOF at each file), the character is displayed on the screen, and then read into the next character. Each read once, the position inside the file indicates a character, and the pointer points to EOF when the file ends. Execute this program will display the entire file.

Second, write a character function FPUTC

The function of the FPUTC function is to write a character written to the specified file, the function call is: fputc (word symbol, file pointer); where the amount of characters to be written can be character constant or variable, for example: FPUTC (' A ', fp); its meaning is to write character A into the file pointed to by the FP.

For the use of the FPUTC function, you should also explain the few points: 1. The file written can be opened, write, read, write, add mode open, write an existing file content when using write or read and write mode, will clear the original file content Write characters from the beginning of the file. If you need to keep the original file content, you want to write the characters to be stored at the end of the file, you must open the file in an additional manner. If the file written does not exist, the file is created. 2. For each of the characters, the internal location of the file will move one byte backward.

3. The FPUTC function has a return value. If the write is successful, returns a word, otherwise returns an EOF. This can be used to determine whether the write is successful.

[Example 10.2] Enter a line from the keyboard to write a file, and then read the file content on the screen. #include main () {file * fp; char ch; if ((fp = fopen ("string", "wt ")) == null) {Printf ("Cannot Open File Strike Any KEY EXIT! "); getCH (); exit (1);} Printf (" INPUT A String: / N "); ch = getchar (); while (ch! = '/ n') {FPUTC (CH, FP); CH = getChar ();} ref (fp); ch = fgetc (fp); while (ch! = EOF) {PUTCHAR (CH); CH = fgetc (fp);} printf ("/ n"); fclose (FP ) The sixth line in the program opens the file string in the way of reading and writing text files. Procedure 13th line After reading a character from the keyboard, enter the loop. When the read characters are not a carriage return, the character is written into the file, and then continue to read the next character from the keyboard. For each character, the file internal location points to move one byte backward. After writing, the pointer has pointed to the end of the file. To read the file from the head, you must move the pointer to the file header, and the procedure 19th REWIND function is used to move the internal position pointer of the file finger from the FP to the file header. Rows 20 to 25 are used to read a line of content in the file.

[Example 10.3] For the files identified by the previous file name in the command line parameter, copy it to the file name identifier, such as only one file name in the command line, writes the file to the standard output file (display) . #include main (int Argc, char * argv []) {file * fp1, * fp2; char ch; if (argc == 1) {Printf ("Have Not Enter File Name Strike Any Key Exit" ); getCH (); exit (0);} = = ((fp1 = fopen (Argv [1], "RT")) == null) {Printf ("canNot open% S / N", argv [1]) GetCH (); exit (1);} if (argc == 2) fp2 = stdout; else if ((fp2 = fopen (argv [2], "wt ")) == null) {Printf ("Cannot Open % S / N ", Argv [1]); getCH (); exit (1);} while ((ch = fgetc (fp1))! = EOF) FPUTC (CH, FP2); fclose (fp1); fclose FP2);} This program is the main function of the belt. Two files fp1 and fp2 are defined in the program, pointing to the files given in the command line parameters, respectively. If the file name is not given in the command line parameter, the prompt information is given. The 18th line of the program means that if only one file name is given, the FP2 points to the standard output file (ie display). The procedure 25 rows to 28 lines of cyclic statements are sent to the file 2 in the read file 1. Run again, a file name (file established by Example 10.2) is given, so output to the standard output file stdout, which is displayed on the display. The third run, give two file names, so read the contents in the String, write to OK. You can use the DOS command TYPE to display OK content: String read and write functions fgets and FPUTS 1. The function of the read string function fgets function is to read a string into the character array from the specified file. The function call is: fgets The character number group name, n, file pointer); N is a positive integer. Indicates that the string read from the file does not exceed N-1 characters. After the last character read, add the string end flag '/ 0'. For example: FGETS (STR, N, FP); meaning is to read the N-1 character sent in the character array STR from the file referred to in the FP. [Example 10.4] A string containing 10 characters is read from the E10_1.C file. #include main () {file * fp; char STR [11]; IF ((fp = fopen ("e10_1.c", "RT")) == null) {Printf ("Cannot Open File Strike any key exit! "); getCH (); exit (1);} fgets (STR, 11, fp); Printf ("% s ", str); fclose (fp);} This example defines an array of characters A total of 11 bytes, after opening the file E101.c in reading a text file, and reads 10 characters from the STR array, and will add '/ 0' within the last unit, then display on the screen. Output STR array. The ten characters output are the top ten characters of the Example 10.1 program.

There are two descriptions for the FGETS function: 1. Before reading the N-1 characters, if you encounter a chart or EOF, the readout ends. 2. The FGETS function also has a return value, and its return value is the first address of the character array. Second, write string function FPUTS

The function of the FPUTS function is to write a string to the specified file, which is: FPUTS (string, file pointer) where the string can be a string, or a character number group name, or a pointer variable, for example: FPUTS ("ABCD", FP); its meaning is to write strings "abcd" into the file referred to in the FP. [Example 10.5] Add a string in the file String established in Example 10.2. #include main () {file * fp; char ch, st [20]; if ((fp = fopen ("string", "at ")) == null) {printf ("Cannot Open file Strike any key exit! "); getch (); exit (1);} Printf (" Input A String: / N "); Scanf ("% S ", ST); FPUTS (ST, FP); REWIND (FP) ); ch = fgetc (fp); while (ch! = EOF) {PUTCHAR (CH); CH = fgetc (fp);} printf ("/ n"); fclose (fp);} This example requires files in String files The end is written in a string, so open the file string in the way of the program's sixth line to add a text file. Then enter the string and write the string into the file string with the FPUTS function. Move the file internal location pointer to the file in the program 15 row. Then enter the cycle one by one to display all the contents in the current file.

Data block read and write functions FREAD and FWRITE

The C language also provides read and write functions for the whole block of data. Can be used to read and write a set of data, such as an array element, a structural variable value, etc. The general form of reading data block function call is: Fread (Buffer, Size, Count, FP); Write data block function calls are: fwrite (buffer, size, count, fp); where buffer is a pointer, in FREAD In the function, it represents the first address of the input data. In the fwrite function, it represents the first address of the output data. Size represents the number of bytes of data blocks. COUNT indicates the number of data blocks to read and written. FP represents the file pointer. For example: FREAD (FA, 4, 5, FP); its meaning is from the file referred to in the FP, each reads 4 bytes (a real number) into the real group FA, continuously reading 5 times, reading 5 Real numbers to fa. [Example 10.6] Enter two student data from the keyboard, written in a file, and then read the data of the two student is displayed on the screen. #include struct stu {char name [10]; int Num; int Age; char addr [15];} Boya [2], Boyb [2], * PP, * QQ; main () {file * fp; char ch; INT i; pp = boya; qq = boyb; if ((fp = fopen ("stu_list", "WB "))) == null) {Printf ("Cannot Open File Strike any key exit!" ); getCH (); exit (1);} printf ("/ ninput data / n"); for (i = 0; i <2; i , pp ) scanf ("% s% D% D% s", PP-> Name, & PP-> Num, & PP-> Age, PP-> Addr); PP = Boya; FWRITE (PP, SIZEOF (Struct Stu), 2, FP); Rewind (fp); FREAD (QQ, Sizeof (STRUCT STU), 2, FP); Printf ("/ n / nName / TNUMBER AGE AGDR / N"); for (i = 0; i <2; i , QQ ) Printf ("% S / T% 5D% 7D% S / N ", QQ-> Name, QQ-> Num, QQ-> AGE, QQ-> AddR); Fclose (fp);} This program defines a structure STU, indicating two structural arrays Boya And BOYB and two structural pointers variables PP and QQ. PP points to BOYA, QQ points to BOYB. Procedure 16th line opens the binary "stu_list" by reading and writing, enters the two student data, write into the file, then move the file internal position pointer to the file, read two student data, on the screen display. Format read and write functions FSCANF and FPRINTF

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

New Post(0)