C++ - 使用 >> 运算符读取文件直到到达行尾

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27595418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:49:22  来源:igfitidea点击:

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

c++operatorsifstreampeekend-of-line

提问by pineapple-na

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.

假设我必须读取一个包含不同类型数据的 txt 文件,其中第一个浮点数是一个 id,然后有一些(并不总是相同数量)其他浮点数代表其他东西......时间,例如, 成对。

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'

但是带有(file.peek() != '\n')条件的 while 循环永远不会自行完成,我的意思是... 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!

有谁知道为什么?或者也许是使用>>运算符读取文件的其他方法?!非常感谢!

回答by user1538798

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

}

回答by SHR

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

要跳过任何字符,您应该尝试在到达之前调用这样的函数 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 istringstreamto parse it.

更好的解决方案是将整行读入字符串而不是用于istringstream解析它。

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

回答by pineapple-na

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.

在您和其他朋友的帮助下,我最终将代码更改为使用 getline()。这是结果,希望它可以帮助某人。

    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!

感谢您的时间!