How can I detect the end of a file in C using stdio and scanf?

  • Thread starter Thread starter rocketboy
  • Start date Start date
  • Tags Tags
    File
AI Thread Summary
To read input from a file in a C program using the scanf function, a common approach is to implement a while loop that continues until the end-of-file (EOF) is reached. This can be done by checking the return value of scanf, which indicates whether the input was successful. If EOF is encountered, the loop will terminate. For scenarios where the number of inputs is unknown, users can also consider alternative methods such as dynamically allocating memory to store inputs or using data structures like arrays or linked lists. Additionally, if the task is related to homework, it is advisable to post in the appropriate section of the forum for targeted assistance.
rocketboy
Messages
243
Reaction score
1
hey,

For this program I am writing in C, I am using stdio and the scanf function to detect input. Now, the person using the program commands in linux for the input to be given from a file, but the problem is I don't know how long the file is. So i have to keep scanf'ing until the file is finished.

Any ideas about how I could do this? I'm not very advanced with C.

Thanks,
-RB
 
Technology news on Phys.org
use a while loop and look for the end of file character (EOF)

while (!EOF)
{
scanf();
}
 
its beautiful.

thanks dr t.
 
It depends on the life size also as well as what you are looking for in the file
The program can run forever till no more resource left for your computer to go on...
 


Hi , I have a question about C. I want to write program that takes numbers as inputs and orders them from maximum to minimum. However, i ve a problem .Count of number is uncertain.I mean ,sometimes i will give the program 5 numbers ,sometimes 10 numbers or another.How can i do this without using eof since i don't know using eof.Please help me ...
 
By the way I use linux
 
Rather than dig up a thread that is 4 1/2 years old, you should start a new thread. If this is a homework problem (and it seems to be), post it in the Homework and Coursework section, in the Engineering and Computer Science section.
 
Back
Top