This article will cover some other functions related to file manipulation, some of which may be less common than those described in the previous two articles.
1. feof () function
Prototype
int feof (FILE *stream);
• Use method: detects the file terminator eof on the stream, returns a non-0 value if detected, or returns 0.
• Example (copy the text content of one file to another file):
#include <stdio.h>int main (void) {file* input = fopen ("d:\\abc.in", "R"); file* output = fopen ("D:\\abc.out", "w"); while (!feof (input)) {fprintf (output, "%c", fgetc (input)); } fclose (stream); return 0;}
2. FGETC () function
Prototype
int fgetc (FILE *stream);
How to use: continuously reads the next character from the file, ends with EOF, and returns a value.
Example: See the example of the feof () function, which has the use of the fgetc () function.
• Correlation function: GetChar () function (this function is read from the STDIN standard stream (normally read from the console))
int GetChar (void);
Getc () function (this function can read multiple (file) streams, while fgetc () can read only one stream)
int getc (FILE *stream);
3. FPUTC () function:
Prototype
int FPUTC (int c, FILE *stream);
• How to use: do not introduce too much, is the reverse process of fgetc (that is, output characters from a file), but the first parameter represents the character to be output
• Example (quote "fputc_ Baidu Encyclopedia "):
#include <stdio.h> #include <string.h>int main () {file*f; Char*s= "hey,buddy!"; int i; F=fopen ("MyFile.txt", "w"); For (I=0;i<strlen (s); i++) FPUTC (s[i],f); Fclose (f); return 0;}
• Correlation function: Putchar (), PUTC () function (also GetChar (), getc () reverse process).
4. Fgets () function:
Prototype
Char *fgets (char *s, int n, FILE *stream);
• How to use: Parameter one represents the first address of the string, parameter two indicates the size of the stored data (that is, the length of the string), the parameter three indicates the
File stream
• Example (quote "fgets_ Baidu Encyclopedia "):
#include <string.h> #include <stdio.h> int main ( void) { file*stream; char string[]= "Thisisatest"; char msg[20];/*open a file for update*/ Stream=fopen ("Dummy.fil", "w+");/*write a string into the file*/ fwrite (String,strlen (String), 1,stream);/*seek to the start of the file*/ fseek (stream,0,seek_set);/*read a string from the file*/ fgets (Msg,strlen (String) +1,stream);/*display the string*/ printf ("%s", msg); fclose (stream); return 0;}
• Correlation function: Gets () function (or input string from console)
5. Fputs () function:
Prototype
int fputs (const char *s, FILE *stream);
• Use method: Fgets () reverse process, string output to the specified file stream
• Correlation function: puts () function
Well, for the time being, more functions can be found in the declarations in the Stdio.h file.
This article from "mlh1719233148 Blog" blog, reproduced please contact the author!
C Language File Operations (3)---Other functions related to file manipulation in stdio.h