从 C++ 中的文件读取直到行尾?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21334173/
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-27 23:36:13  来源:igfitidea点击:

Read from file in c++ till end of line?

c++fileline

提问by user3050163

How can i read data untill end of line?I have a text file "file.txt" with this

如何读取数据直到行尾?我有一个文本文件“file.txt”

1 5 9 2 59 4 6
2 1 2 
3 2 30 1 55

I have this code:

我有这个代码:

ifstream file("file.txt",ios::in);
while(!file.eof())
{
    ....//my functions(1)
    while(?????)//Here i want to write :while (!end of file)
    {
        ...//my functions(2)
    }

}

in my functions(2) i use the data from the lines and it need to be Int ,not char

在我的函数(2)中,我使用行中的数据,它需要是 Int ,而不是 char

回答by herohuyongtao

Don't use while(!file.eof())as eof()will only be set after reading the end of the file. It does not indicate, that the next read will be the end of the file. You can use while(getline(...))instead and combine with istringstreamto read numbers.

不要使用while(!file.eof())aseof()只会在读取文件末尾后设置。它并不表示下一次读取将是文件的结尾。您可以while(getline(...))改用并结合使用istringstream来读取数字。

#include <fstream>
#include <sstream>
using namespace std;

// ... ...
ifstream file("file.txt",ios::in);
if (file.good())
{
    string str;
    while(getline(file, str)) 
    {
        istringstream ss(str);
        int num;
        while(ss >> num)
        {
            // ... you now get a number ...
        }
    }
}

You need to read Why is iostream::eof inside a loop condition considered wrong?.

您需要阅读为什么循环条件中的 iostream::eof 被认为是错误的?.

回答by Some programmer dude

As for reading until the end of the line. there's std::getline.

至于读到行尾。有std::getline

You have another problem though, and that is that you loop while (!file.eof())which will most likely not work as you expect. The reason is that the eofbitflag is not set until afteryou try to read from beyond the end of the file. Instead you should do e.g. while (std::getline(...)).

但是,您还有另一个问题,那就是您的循环while (!file.eof())很可能不会按您的预期工作。原因是eofbit直到您尝试从文件末尾读取之后才设置该标志。相反,你应该做 eg while (std::getline(...))

回答by Rakia

char eoln(fstream &stream)          // C++ code Return End of Line
{
    if (stream.eof()) return 1;     // True end of file
    long curpos;    char ch;
    curpos = stream.tellp();        // Get current position
    stream.get(ch);                 // Get next char
    stream.clear();                 // Fix bug in VC 6.0
    stream.seekp(curpos);           // Return to prev position
    if ((int)ch != 10)              // if (ch) eq 10
        return 0;                   // False not end of row (line)
    else                            // (if have spaces?)
        stream.get(ch);             // Go to next row
    return 1;                       // True end of row (line)
}                                   // End function

回答by Rakia

If you want to write it as function in order to call some where, you can use a vector. This is a function which I use to read such file and return integers element wise.

如果你想把它写成函数以便调用一些地方,你可以使用向量。这是我用来读取此类文件并明智地返回整数元素的函数。

vector<unsigned long long> Hash_file_read(){
    int frames_sec = 25;
    vector<unsigned long long> numbers;
    ifstream my_file("E:\Sanduni_projects\testing\Hash_file.txt", std::ifstream::binary);
    if (my_file) {

        //ifstream file;
        string line;

        for (int i = 0; i < frames_sec; i++){
            getline(my_file, line);
            numbers.push_back(stoull(line));
        }

    }
    else{
        cout << "File can not be opened" << endl;
    }
    return numbers;
}