Home > Back-end >  File reading problems
File reading problems

Time:05-12


Hh file content is asd newline, two questions want to ask next everybody

Asd is 1. If my hh file content, don't add newlines, is can output the content of the ideal, but if the file content is asd, a new line will somehow more output a line, the following for the output as a result, under normal circumstances should only output the first and third lines, why can a newline character difference will be more out of the middle line?

Line 1 has three characters, a word
Line 2 has two characters, one word
A total of 2, 5 characters, two words

2. Then I found the reason is for debugging after reading the first row, he ran to the file pointer to the first line beginning, again read data bank, but not repeat read '\ n' again, according to my understanding before after he finished reading the first line of the file pointer is automatically to the second line, found then exits the EOF marker to the second line, what went wrong understand

CodePudding user response:

Function and have no long eyes, and it can't hit the end of the file before knew ahead is the end of the file,
Your cycle for the first time, not to the end, read in the first row, remove the characters (at that time is a newline) at the end of the remaining normal characters in STR,
Never hit end-of-file, cycle for the second time, so the condition is true, to enter the loop body, the fgets reads new line failure, there are two consequences, the first is to hit end-of-file, next feof will return true; Second is not updated STR, or the original content, then removed at the end of the characters, according to your statement at this time to remove the 'd', said the file pointer position is read, it doesn't return to the beginning, and in the end,
Loop condition for the third time, because the feof is true, so the loop condition is false, skip cycle,
In conclusion,
Write code at the end is always empty when problems and suggestion of four teachers in changes, the responses of the other posts,

CodePudding user response:

Thank you for your patience to answer, I want to ask can you think of the file contents as asd \ nEOF, first the file pointer to a, each read one character at a time in the future one, if so, the end of the first cycle p is not pointing to the EOF, second cycle judgment feof return 1 out of circulation
  • Related