English 中文(简体)
阅读C++文本档案中的分类账
原标题:Read integers from a text file with C++ ifstream
  • 时间:2011-11-14 02:56:41
  •  标签:
  • c++

我想读一下文字档案中的图表紧急信息,将其储存成病媒。

  • 档案有任意数量线

  • each line has arbitrary number of integers ended with

例如,

First line:
0 1 4
Second line:
1 0 4 3 2
Thrid line:
2 1 3
Fourth line:
3 1 2 4
Fifth line:
4 0 1 3

If I use getline() to read one line at a time, how do I parse the line (as each line has variable number of integers)?

任何建议?

最佳回答

标准行文:

#include <fstream>
#include <sstream>
#include <string>
#include <vector>


std::ifstream infile("thefile.txt");
std::string line;

while (std::getline(infile, line))
{
  std::istringstream iss(line);
  int n;
  std::vector<int> v;

  while (iss >> n)
  {
    v.push_back(n);
  }

  // do something useful with v
}

此处采用<条码>的单线版本。 我们需要一个辅助性建筑(信用到@Luc Danton!) ,这与<代码”相反:move:

namespace std
{
  template <typename T> T & stay(T && t) { return t; }
}

int main()
{
  std::vector<std::vector<int>> vv;

  for (std::string line;
       std::getline(std::cin, line);
       vv.push_back(std::vector<int>(std::istream_iterator<int>(std::stay(std::istringstream(line))),
                                     std::istream_iterator<int>())
                    )
       ) { }

  std::cout << vv << std::endl;
}
问题回答

第一行改为“<代码>std:getline function, 然后使用std:stringstream,改为:

std::ifstream file("input.txt");

std::vector<std::vector<int>> vv;
std::string line;
while(std::getline(file, line))
{
    std::stringstream ss(line);
    int i;
    std::vector<int> v;
    while( ss >> i ) 
       v.push_back(i);
    vv.push_back(v);
}

You can also write the loop-body as:

while(std::getline(file, line))
{
    std::stringstream ss(line);
    std::istream_iterator<int> begin(ss), end;
    std::vector<int> v(begin, end);
    vv.push_back(v);
}

这样做的时间越短,越好。 或合并后两行:

while(std::getline(file, line))
{
    std::stringstream ss(line);
    std::istream_iterator<int> begin(ss), end;
    vv.push_back(std::vector<int>(begin, end));
}

现在,它不会缩短,因为它会ug。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签