【问题标题】:C++ How to I read column from text file? [closed]C++ 如何从文本文件中读取列? [关闭]
【发布时间】:2017-12-18 17:38:19
【问题描述】:

我有一个 C++ 项目,需要从文本文件中找到最大的文件名。 我的文本文件示例是:

foundedindex = instline.find("  ");
    inst_host = instline.substr(0, foundedindex);
    //cout << inst_host << " a" << endl;
    obj[count].sethost(inst_title);

所以,我只想阅读“index.html”、“23,html”、“24.html”等。 当我在代码中逐行分隔时,排序需要太多时间。 请帮帮我。

【问题讨论】:

  • 请在此处以纯文本形式发布代码、错误、示例数据或文本输出,而不是难以阅读的图像,不能复制粘贴以帮助测试代码或在答案中使用,并且对使用屏幕阅读器的人怀有敌意。您可以编辑问题以在问题正文中添加代码。使用{} 按钮来格式化任何代码块,或者使用四个空格缩进以获得相同的效果。
  • 使用std::getline()std::istringstream 和提取运算符&gt;&gt; 仅获取输入行的一部分。
  • 你好像把代码和数据搞混了。

标签: c++ file sorting text


【解决方案1】:

这是一种获取每一行的列的方法。

std::string temp;
std::vector<vector<std::string>> data;
while(std::getline(file, temp))
{
    std::vector<std::string> x;
    std::istringstream liney(temp);
    while(std::getline(liney, temp, ' '))
    {
        x.push_back(temp);
    }
    data.push_back(x);
}
// Then using a column loop through a 2d array.
int row = 0;
for(int i = 0; i<data[row].size(); i++)
{
    for(int s = 0; s<data.size(); s++)
    {
       data[s][i]; // Do something
    }
    row++;
}

希望这会有所帮助。

【讨论】:

  • @user0042 我编辑了我的帖子以反映您的建议
  • @user0042 很抱歉,这应该很好。
  • @Alkyonemis 如果可行,请点赞并接受。
猜你喜欢
  • 1970-01-01
  • 2017-05-24
  • 2015-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 2014-07-19
  • 1970-01-01
  • 2013-03-07
相关资源
最近更新 更多