End of File(EOF)

Hi, I want to use eof() function to stop reading datas from a file when it reaches to end of data. It works well except one case, which is when the file is empty. It takes a random number and make the program crashed. What can I do about that? Am I doing something wrong?

1
2
3
4
5
6
if( !inputFile.eof( ) )
{
inputFile >> num;
cout << num;
numPrevious = num;
}
eof() is false, because you haven't attempted to read anything.

The function returns true if the eofbit stream's error flag has been set by a previous i/o operation

http://www.cplusplus.com/reference/ios/ios/eof/
1
2
	ifstream inputFile("compout.txt");
	ofstream outputFile("decompout.txt");


This part reads the file.
inputFile >> num;

Is there anyone who knows solution? Please I need to finish the project.

This part reads the file.


That is at line 3. The if statement is at line 1.
When you execute the if statement, you haven't yet attempted to read from the file.
Read what I quoted carefully. "flag [is] set by a previous i/o operation". You've performed no previous I/O operation.
Is there anyone who knows solution?

Don't use the eof() function for something it was never meant to be used:

1
2
3
4
5
if( inputFile >> num )
{
    cout << num;
    numPrevious = num;
}
@AbstractionAnon

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	ifstream inputFile("compout.txt");
	ofstream outputFile("decompout.txt");
	
	int num, numPrevious;
	if( !inputFile.eof( ) )
	{
		inputFile >> num;
		cout << num;
		numPrevious = num;
		outputFile << dictionary[num].key;

	}
	while( !inputFile.eof( ) ) 
	{

		inputFile >> num;
      .
      .
      .
      continues


It is not at line 3, I just wanted to show that I read the line. Do not worry about this part, I know how to read a file. As I said it works very well, but when the file is empty it crashes. What I am asking is when the file is empty.

@Cubbi sorry, I have not shared the code so everybody confused. Here is the real one. I need to use eof somehow. Project says to use it.
Last edited on
Program shouldn't enter the inside of if statement. File is empty, I don't get it?
I need to use eof somehow. Project says to use it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    if( inputFile >> num )
    {
        cout << num;
        numPrevious = num;
        outputFile << dictionary[num].key;
    }

    if(inputFile.eof())
    {
        std::cout << "Enf of file reached before reading the first value\n";
        return /* whatever your function returns */;
    }

    while( inputFile >> num )
    {

      .
      .
      .
      continues
    }

    if(inputFile.eof())
    {
        std::cout << "Enf of file reached successfully\n";
    }


Last edited on
It is not working again, program does not enter the if statement. There should be something that checks if the file is empty or not? EOF does not do it.
By the way, I said earlier I need to use eof. I used already in while loop, this if statement is for only if the file is empty or not. So, is there any other way, I can use it. Not just eof.
Topic archived. No new replies allowed.