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

  • Thread starter rocketboy
  • Start date
  • Tags
    File
In summary, the conversation discusses using stdio and the scanf function in a C program to detect input from a file. The problem is that the length of the file is unknown, so the program needs to keep scanning until the end of file character (EOF) is reached. The suggestion is to use a while loop to continuously scan until the end of file is reached. Additionally, there is a question about ordering numbers from maximum to minimum with an uncertain number of inputs, which may require using eof.
  • #1
rocketboy
243
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
  • #2
use a while loop and look for the end of file character (EOF)

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

thanks dr t.
 
  • #4
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...
 
  • #5


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 ...
 
  • #6
By the way I use linux
 
  • #7
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.
 

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

1. How do you detect the end of a file in C?

The end of a file in C can be detected by using the feof() function. This function checks if the end-of-file indicator has been set for a given file stream, indicating that there is no more data to be read from the file.

2. What is the syntax for using feof() in C?

The syntax for using feof() in C is:
int feof(FILE *stream);
This function takes in a file stream as a parameter and returns an integer value of 0 if the end-of-file indicator is not set, or a non-zero value if it is set.

3. Can you give an example of using feof() to detect the end of a file?

Yes, here is an example of using feof() in C to detect the end of a file:
FILE *fp; // file pointer
char ch;
fp = fopen("myfile.txt", "r"); // open file for reading
if(fp == NULL) {
// error opening file
} else {
// read characters until end of file
while(!feof(fp)) {
ch = fgetc(fp);
// do something with the character
}
fclose(fp); // close file
}

4. What happens if feof() is called before reading from a file?

If feof() is called before reading from a file, it will return a non-zero value, indicating that the end-of-file indicator is already set. This means that there is no more data to be read from the file and it is safe to exit the loop or function.

5. Is feof() the only way to detect the end of a file in C?

No, feof() is not the only way to detect the end of a file in C. Another way is to use the fscanf() function, which returns the number of items successfully read from a file. If this value is less than the specified number of items, it means the end of the file has been reached. Additionally, some file reading functions, such as fgets() and fgetc(), return NULL or EOF when the end of a file is reached.

Similar threads

Back
Top