A common C language exercise:
"Use fgets () to read the content of an ASCII text file and output all the text to the console line by line ."
Can you do this easily? Show SectionCodeLet's take a look.
Is the code you wrote 100% correct? Will there be major defects? Focus on the following:
-Is a bunch of garbled characters output when the file content is empty (the length is zero?
-Does the last line of text be repeatedly output when the object ends with a line break?
-If there is no line change at the end of the file, does the last line have no output?
-Have you considered that a long line of text may cause incomplete reading?
-Do you realize that if fgets () encounters an ASCII character 26 (0x1a), it means that the end of the file (feof () returns 1 )?
-Do you realize that all the text read by fgets () ends with '\ n \ 0' (except the last line )?
-Does UNIX, Linux, windows, Mac, and other systems use different line breaks (\ n or \ r \ n )?
Here I will give you an incomplete reference answer. I am afraid I will not score 70 points (100 points ):
F = fopen ("file.txt", "R"); If (f) {char line [1024]; while (1) {int Len; line [0] = '\ 0'; fgets (line, sizeof (line), f); If (LEN = sizeof (line)-1) continue; // too long line, ignore itprintf ("% s", line); If (feof (F) break;} fclose (f );}
Additional description: to be continued