The Fget function is prototyped as follows: Char *fgets (char *buf, int n, FILE *fp)
Function: Reads a line from the file stream, sends to the buffer, when using the note following points:
1. When encountering newline character or buffer is full, fgets will stop, return to read the data, it is worth noting that fgets can not read binary files, because fgets will be binary files as a text file to deal with, this will inevitably produce garbled.
2. Each call, Fgets will subscript character The last word of the buffer to null, which means that the last character cannot be used to hold the required data, so if there is a row containing line_size characters (including line breaks), to read this line into the buffer, please set the parameter N to Line_ Size+1
3. The conclusion 1 can be introduced: given the parameter n,fgets can only read n-1 characters (including line breaks), if there is a row more than n-1 characters, then Fgets returns an incomplete line, that is, only read the first n-1 characters of the line, but the buffer always ends with a null character, The next call to Fgets will continue to read the row.
Examples of usage:
Read a file and print it with the filename * fp = fopen (Argv[1], "R"); Char buf[200]; while (fgets (BUF,200,FP) = NULL) {/* characters per line cannot exceed 199 to work correctly */
printf ("%s", buf); }
Note that printing does not use printf ("%s/n", buf), because the Fgets function is used to read the string from the file. The Fgets function is called as follows: Fgets (STR,N,FP); here, the FP is the file pointer; Str is the starting address of the string, and n is an int type variable. function is the function of the file from the FP read into the n-1 characters into the STR as the starting address of the space, if the n-1 characters are not read, read a line break or an EOF (file end flag), the end of this read operation, the read in the string is the last to include read the newline character. So, to be exact, you can only read n-1 characters when you call the Fgets function. At the end of the read, the system will automatically add ' + ' at the end and return with STR as the function value. For it will also read the line break in