The fputc function writes a character to a specified file. The function is called in the form of fputc (character quantity, file pointer, the number of characters to be written can be a character constant or variable, for example, fputc ('A', FP). It means to write character a to the file pointed to by FP.
Use of fputc functions:
1. the written file can be opened by using, writing, reading, and writing. When an existing file is opened by writing or reading/writing, the original file content will be cleared, the write character starts from the beginning of the file. To retain the content of the original file and store the characters to be written at the end of the file, the file must be opened in append mode. If the file to be written does not exist, the file is created.
2. Each time a character is written, the internal position pointer of the file moves one byte backward.
3. The fputc function has a return value. If the write is successful, the written character is returned. Otherwise, an EOF is returned. This can be used to determine whether the write is successful.
Use of the fgetc function:
Enter a line of characters from the keyboard, write a file, and read the file content on the screen.
# Include <stdio. h>
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 ();
}
Rewind (FP );
Ch = fgetc (FP );
While (Ch! = EOF)
{
Putchar (CH );
Ch = fgetc (FP );
}
Printf ("/N ");
Fclose (FP );
}
The second line in the program opens the string file in the form of reading and writing text files. The program reads a character from the keyboard in line 3 and then enters the loop. When the character is not a carriage return, the program writes the character into the file and continues to read the next character from the keyboard. Each time one character is entered, the internal position pointer of the file moves one byte backward. After writing, the pointer is directed to the end of the file. To read the file from the beginning, you must move the pointer to the file header. The program's 19th-line rewind function is used to move the internal position pointer of the file referred to by FP to the file header. Lines 20th to 25 are used to read a row of content from the file.
Read character function fgetc
The fgetc function is used to read a character from a specified file. The function is called in the form of: character variable = fgetc (File pointer); for example: CH = fgetc (FP ); it means reading a character from the open file FP and sending it to Ch.
The usage of the fgetc function is described as follows:
1. In the fgetc function call, the read file must be opened in read or read/write mode.
2. The result of reading a character can also be not assigned to the character variable, for example, fgetc (FP). However, the read characters cannot be saved.
3. There is a position pointer inside the file. It is used to point to the current read/write bytes of the file. When a file is opened, the pointer always points to the first byte of the file. After the fgetc function is used, the position pointer moves one byte backward. Therefore, you can use the fgetc Function Multiple times to read multiple characters. Note that the file pointer is not the same as the position pointer inside the file. The file Pointer Points to the entire file and must be defined in the program. As long as no value is assigned again, the file pointer value remains unchanged. The position pointer inside the file is used to indicate the current read/write position inside the file. Every read/write time, the pointer moves backward. It does not need to be defined in the program, but is automatically set by the system.
Read the file e10-1.c and output on the screen.
# Include <stdio. h>
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 );
}
Copy the file marked by the previous file name in the command line parameter to the file marked by the last file name. For example, if there is only one file name in the command line, write the file to the standard output file (Display).
# Include <stdio. h>
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 );
}
If (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 with parameters. The program defines two file pointers: FP1 and fp2, respectively pointing to the file given in the command line parameters. If the command line parameter does not provide a file name, a prompt is displayed. Line 2 of the program indicates that if only one file name is given, fp2 is directed to the standard output file (Display ). The program reads the characters in file 1 one by one with loop statements from lines 2 to 28 and then sends them to file 2. During the re-running, a file name (the file created in example 10.2) is provided, so stdout is output to the standard output file, that is, the file content is displayed on the display. In the third run, two file names are provided, so the content in the string is read and written to OK. Use the doscommand type to display OK content