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
Click For Summary

Discussion Overview

The discussion revolves around detecting the end of a file in C using the stdio library and the scanf function. Participants explore methods to handle input from a file of uncertain length, particularly in a Linux environment.

Discussion Character

  • Exploratory, Technical explanation, Homework-related

Main Points Raised

  • One participant suggests using a while loop to continuously call scanf until the end of file (EOF) is reached.
  • Another participant comments on the potential for the program to run indefinitely if not properly managed, indicating concerns about resource usage.
  • A separate participant raises a related question about handling an uncertain number of inputs for a different program, specifically asking for alternatives to using EOF.
  • One participant advises against reviving an old thread and suggests starting a new one for clarity, particularly if the question is related to homework.

Areas of Agreement / Disagreement

There is no clear consensus among participants. Some propose methods for detecting EOF, while others express concerns about program behavior and resource management. Additionally, a separate question about input handling introduces further complexity without resolution.

Contextual Notes

Participants do not provide specific details on the implementation of EOF detection or alternative methods for handling input, leaving some assumptions and definitions unresolved.

Who May Find This Useful

Individuals interested in C programming, particularly those dealing with file input and output, as well as students seeking help with homework-related programming challenges.

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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
81
Views
7K
  • · Replies 9 ·
Replies
9
Views
4K
Replies
35
Views
7K
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K