C++ - Read file until reaching end of line with >> operators

33.3k views Asked by At

I have looked around a lot, and still not found how to do this, so, please bear with me.

Let's say i have to read a txt file that contains different kinds of data, where the first float is an id, and then there are a few (not always the same amount) of other floats representing other stuff... times, for example, in pairs.

So the file would look something like:

1 0.2 0.3
2.01 3.4 5.6 5.7
3 2.0 4.7
...

After a lot of research, I ended up with a function like this:

vector<Thing> loadThings(char* filename){
    vector<Thing> things;
    ifstream file(filename);
    if (file.is_open()){
        while (true){
            float h;
            file >> h; // i need to load the first item in the row for every thing
            while ( file.peek() != '\n'){

                Thing p;
                p.id = h;
                float f1, f2;
                file >> f1 >> f2;
                p.ti = f1;
                p.tf = f2;

                things.push_back(p);

                if (file.eof()) break;
            }
            if (file.eof()) break;
        }
        file.close();
    }
return things;
}

but the while loop with the (file.peek() != '\n') condition never finishes by itself, i mean... peek never equals '\n'

does anybody have an idea why? Or perhaps some other way to read the file using >> operators?! Thank you very much!

3

There are 3 answers

1
user1538798 On

just suggesting another way, why not use

// assuming your file is open
string line;

while(!file.eof())
{
   getline(file,line);

  // then do what you need to do

}
0
SHR On

To skip any character you should try to call a function like this before reaching the while(file.peek() != '\n')

istream& eatwhites(istream& stream)
{
    const string ignore=" \t\r"; //list of character to skip
    while(ignore.find(stream.peek())){
        stream.ignore();
    }
    return stream;
}

A better solution is to read the whole line into string than use istringstream to parse it.

float f;
string line;
std::getline(file, line);
istringstream fin(line)
while(fin>>f){ //loop till end of line
}
1
pineapple-na On

With a little help from you and other friends i ended up changeing the code to use getline() instead. Here is the result, hope it helps someone.

    typedef struct Thing{
        float id;
        float ti;
        float tf;
    };

    vector<Thing> loadThings(char* filename){
        vector<Thing> things;
        ifstream file(filename);
        if (file.is_open()){

            string line;
            while(getline(file, line))
            {
                istringstream iss(line);
                float h;
                iss >> h;
                float f1, f2;
                while (iss >> f1 >> f2)
                {
                    Thing p;
                    p.id = h;
                    p.ti = f1;
                    p.tf = f2;

                    things.push_back(p);
                }
            }
            file.close();
        }
        return things;
    }

Thank you for your time!