why when using fscanf to acquire data from a file, is used 2 times, once before the “!feof(fin)” and later, as shown in the code below:
fscanf(fin, "%s", letta);
while(!feof(fin)){
fscanf(fin, "%s", letta);
}
the word read is not the same?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No the word read would not be the same since you read from the file twice, you would get different data each time.
The condition in the
whiletests to see if you are at the end of file, in order to do that a read attempt must have been made, which requires thefscanf()before the loop.Once you are inside the loop you want to continue reading from the file. Presumably there would be more code inside the loop to process the data you are reading.
In the code you have posted, if you encountered the end of file with your first read attempt, you wouldn’t enter the (
while) loop.Contrast this with a
do-whileconstruct where the test is at the bottom of the loop (ie the read occurs only once, at the “bottom” of the loop). There you will always enter the loop at least once.