【问题标题】:Better way of handling of fscanf equivalent in c++在 c++ 中处理 fscanf 等价物的更好方法
【发布时间】:2016-07-22 08:12:28
【问题描述】:

我正在尝试找出处理文本输入的最佳方式,就像我在 C 中使用 fscnaf 一样。

以下似乎适用于包含...的文本文件

string 1 2 3
string2 3 5 6

我也想要。它读取每行上的各个元素并将它们放入各自的向量中。你会说这是处理输入的好方法吗?输入总是以一个字符串开头,然后在每行后面跟着相同数量的数字。

int main(int argc, char* argv[])
{
ifstream inputFile(argv[1]);

vector<string> testStrings;
vector<int> intTest;
vector<int> intTest2;
vector<int> intTest3;
string testme;
int test1;
int test2;
int test3;

if (inputFile.is_open())
{
    while (!inputFile.eof())
    {
        inputFile >> testme;
        inputFile >> test1;
        inputFile >> test2;
        inputFile >> test3;

        testStrings.push_back(testme);
        intTest.push_back(test1);
        intTest2.push_back(test2);
        intTest3.push_back(test3);
    }
    inputFile.close();
}
else
{
    cout << "Failed to open file";
    exit(EXIT_FAILURE);
}
return 0;
}

更新

我已将 while 循环更改为这个...更好了吗?

    while (getline(inputFile, line))
    {
        istringstream iss(line);

        iss >> testme;
        iss >> test1;
        iss >> test2;
        iss >> test3;

        testStrings.push_back(testme);
        intTest.push_back(test1);
        intTest2.push_back(test2);
        intTest3.push_back(test3);
    }

【问题讨论】:

  • 如果您尝试处理来自文本文件的输入,该文件包含单独的文本行,每行文本以换行符终止,则解析此输入的适当方法是使用 std: :getline() 读取每一行。这就是 std::getline() 的用途。只有在读完整行之后,您才应该尝试以某种方式解析其内容。从读取行构造std::istringstream 是一种合理的方法,但这不是唯一的方法。
  • 我试过了,但对如何处理 getline 感到困惑。我之前处理它的方式是在我使用 getline 之后逐个字符地检查。我正试图摆脱这一点。
  • while (!inputFile.eof()) 会让你失望。在这里阅读更多:stackoverflow.com/questions/5605125/…
  • 您是说也要使用 getline 吗?到目前为止它有效。不是说我的做法是正确的——这就是我在这里的原因。
  • 我对循环进行了更改 - 你们介意检查一下吗?

标签: c++ file input


【解决方案1】:

对于您的代码,请阅读以下内容:Why is iostream::eof inside a loop condition considered wrong?


既然您知道格式,请使用ifstream,您可以轻松编写更少的代码来实现相同(或更好的结果):

#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]) {
        std::ifstream ifs;
        if(argc > 1) {
                ifs.open(argv[1]);
        } else {
                std::cout << "Usage: " << argv[0] << " <filename>\n";
                return -1;
        }
        std::string str;
        int v1 = -1, v2 = -1, v3 = -1
        if (ifs.is_open()) {
                while(ifs >> str >> v1 >> v2 >> v3)
                        std::cout << str << ' ' << v1 << ' ' << v2 << ' ' << v3 << std::endl;
        } else {
                std::cout << "Error opening file\n";
        }
        return 0;
}

输出:

gsamaras@gsamaras:~$ g++ -Wall readFile.cpp 
gsamaras@gsamaras:~$ ./a.out test.txt 
string 1 2 3
string2 3 5 6

我受到了启发:How to read formatted data in C++?

【讨论】:

  • 所以我基本上可以跳过 getline 并让 while 循环检查我得到的内容,因为如果预期的输入不正确,它将失败?没想到但有道理!也没有想到只将 ifstream 放在一行上。谢谢!
  • 在 SO 中提出与您类似的问题之前,我现在也没有这样做,当时有人回答,让我知道,现在我是让你知道的人!确保您以后让其他人知道@cpd1! :D
  • 啊,太好了...提前付款 :) 会尝试做同样的事情...谢谢!
  • @cpd1 这让我想起了“Pay It Forward”,这部电影,我会为你喜欢的答案投票,因为这是一部非常感人的电影!
猜你喜欢
  • 1970-01-01
  • 2015-06-24
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-09
  • 2022-10-15
  • 2011-06-05
相关资源
最近更新 更多