file.eof and tabs

I have

1
2
3
while(!file.eof() ){
//does something
}


in file there is a set of measurements
last measurements is followed by a tab space

my question is will the .eof become true after reading the last number or after reading the tab?
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) {
Last edited on
I tend to use: while( inFile.good() ).

Just my two cents. (:
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
Last edited on
Quote from this site:
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

input file is in the quote below

1.64638
error
1.55023
1.54612
1.54004
1.60666
1.48665
1.64324
1.51259
1.56718
1.65898
1.46271
1.61033
1.67845
1.60833
1.48771
1.61548
1.47745
1.51166
1.65934
1.55124
1.63349
1.59141
1.64520
1.55601
1.63830
1.57711
1.58464
1.65309
1.67066
1.63383
1.60722
1.74772
1.53712
1.77965
1.72866
1.39334
1.79338
1.62130
1.30483
1.71398
1.74054
1.57716
1.64926
1.62492
1.69285
1.59722
1.70241
1.40685
1.46920
1.69859

Last edited on
Does there need to be the trailing tab? Or can you remove it?

Is this file generated by your program?
why?

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.
Last edited on
the file was given to me by my professor.
if I cancel the tab it works.

however I would like to be able to use a file that has a trailing tab
Ok. Are you using std::getline( file, std::string, delimiter );

If so, you could do a simple check.

1
2
3
4
std::getline( file, input, '\n' );

if( input == '\t');
    break;
Last edited on
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;
    } else if(file.eof()) {
        std::cout << "file ended, only " << n << " numbers were read\n";
        break;
    } else if(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.
Last edited on
@Lynx thanks that would work.

@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)
Topic archived. No new replies allowed.