c++ ifstream,检测字母还是EOLine?

发布于 2024-07-12 19:27:14 字数 371 浏览 4 评论 0原文

我有这个函数可以从文件中读取所有整数。 问题是,当我读字母时,我会触发一个新行,并且我总是按 1 查找而不是到行尾。 我怎样才能更好地编写这个功能?

int v;
    while (!in.eof())
    {
        while (in >> v)
            cout << v << " ";

        cout << endl;
        if (in.eof())
            break;
        in.clear();
        in.seekg(1, ios::cur);
        int a;
        a=0;
    }

I have this function to read in all ints from the file.
The problem is when i read letters i trigger a new line and i always seek by 1 and not to the end of line. How can i write this function better?

int v;
    while (!in.eof())
    {
        while (in >> v)
            cout << v << " ";

        cout << endl;
        if (in.eof())
            break;
        in.clear();
        in.seekg(1, ios::cur);
        int a;
        a=0;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初懵 2024-07-19 19:27:21

第一个问题是上面的代码没有在行尾停止。
运算符>>() 忽略包含换行符的“空白字符”。

怎样才能写得更好?
如果不知道您要对这些数字做什么以及文件的输入格式是什么,就很难判断!

但如果我写这个,我会在不使用这些行的情况下编写它:

    if (in.eof())
            break;
    in.clear();
    in.seekg(1, ios::cur);

处理格式化文本文件时,很少看到使用eek()。

假设:
文件仅包含数字。
您想要将所有数字读入一个容器中。

std::vector<int>    data;

std::copy(  std::istream_iterator<int>(in),  // An iterator from current point
            std::istream_iterator<int>(),    // To end of file.
            std::inserter(data)              // Destination (insert into data)
         );

First problem the above code does not stop at the end of line.
The operator>>() ignores "White Space Characters" which includes the new line character.

How can this be written better?
That is hard to tell without knowing what you are trying to do with the numbers and what the input format of the file is!

But If I were writting this I would write it without using these lines:

    if (in.eof())
            break;
    in.clear();
    in.seekg(1, ios::cur);

When processing a formated text file it is rare to see the use of seek().

Assumign:
File contains just numbers.
You want to read all the numbers into a container.

std::vector<int>    data;

std::copy(  std::istream_iterator<int>(in),  // An iterator from current point
            std::istream_iterator<int>(),    // To end of file.
            std::inserter(data)              // Destination (insert into data)
         );
冷夜 2024-07-19 19:27:19

如果您的文件仅包含由空格(包括)换行符分隔的整数,那么这应该足够了。

while( in >> v )
{
    // do something with v
}

在文件之后,如果 in.fail() 为 false 并且 in.eof() 为 true,则到达文件末尾且没有格式错误。 否则读取 int 时会发生错误。

如果您收到无效输入并希望从中恢复,那么您需要弄清楚如何恢复。 如果你想跳到行尾并再次开始解析,你可以使用类似的东西。

in.clear();
in.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

If your file consists of just ints separated by whitespace (including) newlines then this should be sufficient.

while( in >> v )
{
    // do something with v
}

After the file, if in.fail() is false and in.eof() is true, then you reached the end of the file without a formatting error. Otherwise an error reading an int occurred.

If you receive invalid input and want to recover from that then you need to work out how you want to recover. If you want to skip until the end of the line and start parsing again, you can use something like this.

in.clear();
in.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文