【问题标题】:Read from file in c++ till end of line?从c ++中的文件读取直到行尾?
【发布时间】:2014-01-24 13:38:25
【问题描述】:

我怎样才能读取数据直到行尾?我有一个文本文件“file.txt”

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

我有这个代码:

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)
    }

}

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

【问题讨论】:

  • 为什么要读到行尾? “行尾”是指 first 行的结尾,还是所有行的结尾?
  • 通过“行尾”我的意思是阅读第一行,使用数字做我必须做的事情并继续第二行......,然后在第三行直到结束文件
  • 你是否对每一行执行相同的操作?
  • 当前的答案有帮助吗?请提供反馈。
  • 不,他们没有帮助。 herohuyongtao 写的代码不正确。我再重复一遍:我想从第一行到最后一个一个地取出元素并对其进行操作。然后我对第二行做同样的事情和同样的操作......

标签: c++ file line


【解决方案1】:

不要使用while(!file.eof()),因为eof()只会在读取文件末尾后设置。它并不表示下一次读取将是文件的结尾。您可以改用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 ...
        }
    }
}

你需要阅读Why is iostream::eof inside a loop condition considered wrong?

【讨论】:

  • 如果我使用 getline 我必须从 int 转换为 char,但我需要数据是 int !!!
  • @user3050163 已更新。
  • (getline(file, str)) is not working error:can;t convert from istream to char
  • 错误 1 ​​错误 C2664: 'std::basic_istream<_elem> &std::basic_istream<_elem>::getline(_Elem *,std::streamsize)' : 无法转换参数 1从 'std::ifstream' 到 'char *'
  • @user3050163 应该可以运行,我刚刚在ideone 上测试过。
【解决方案2】:

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

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

【讨论】:

【解决方案3】:
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

【讨论】:

  • 您可以使用它来阅读直到该行的其余部分: string line;getline(stream, line); .我用它来读取文件的剩余部分
【解决方案4】:

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

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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多