file.eof() will only become true after reading past the end of file and failing to retrieve anything because of that. Why do you have such a loop in the first place? It's almost always an error.
Check if the input operation succeeded after attempting the input, not before, and check all error bits. The usual C++ idiom for this is while(file >> var) {
no sorry I don't understand
how should I use while(file>>var)?
why? I want to read measurements and write them into an array
I tried first with a for loop but then I wanted to do so without having to specify the number of measurements
i thought to tell the program to keep reading measurements until eof but !file.eof() is still true after reading the last element
edit: @Lynx876 while( file.good() ) gives me the same problem, it reads the file 1 extra time
bad()
Returns true if a reading or writing operation fails. For example in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.
fail()
Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end. good()
It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true.
It should be returning false.
Can you post your input file? Or if it's too big, just some up until the end.
I cannot use good() otherwise it would skip the bad inputs;
I check for fail() within the while loop in order to print an error message for the the value 'error'. I use the input operator >> in my while loop
Because it's both correct and idiomatic (that is, taught by all good textbooks and understood by other C++ programmers).
I am what to read measurements and write them into an array
Normally you'd use a vector in this case:
1 2
while(file >> var) // read from file
vect.push_back(var) // if successful, append to the vector
With an array, you need to keep incrementing the index (or pointer) and checking for the end of the array:
1 2 3 4 5 6
int n = 0;
while(n < size && file >> var) // if n isn't too big, read from file
{
arr[n] = var; // if successful, write to the array
++n; // this can be combined with the lines above in various ways
}
i thought to tell the program to keep reading measurements until eof but !file.eof() is still true after reading the last element
Exactly. The same is true of good() and other stream status flags. They do not predict the future.
I check for fail() within the while loop in order to print an error message for the the value 'error'.
Oh, I didn't see that when I wrote the simple loops.
if you have to do different actions based on different status flags, AND you're continuing to process the input in some cases, then there isn't much you can check about the stream in the beginning of the loop at all:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int n = 0;
while(n < size)
{
if(file >> arr[n]) {
++n;
} elseif(file.eof()) {
std::cout << "file ended, only " << n << " numbers were read\n";
break;
} elseif(file.fail()) {
std::cout << "error detected\n";
file.clear();
file.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
The key point is that fail(), bad(), eof(), and good() only make sense after an I/O operation.
@Cubbi seems like the code that I am looking for, but how can I check the size of the file?
in my program user inputs the number of measurements (50) but in the file there are actually 50+error (+tab?) elements
I used eof so that because I did not know how to find the size
ps sorry for the 'why' was a rhetorical question, and also the spelling mistake (edit now)