如何在 C++ 中逐行读取文件中的整数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2287121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:53:24  来源:igfitidea点击:

How to read groups of integers from a file, line by line in C++

c++input

提问by Peter Smit

I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are.

我有一个文本文件,每一行都有一个或多个整数,用空格分隔。我怎样才能用 C++ 以优雅的方式阅读这个?如果我不关心我可以使用 cin >> 的行,但重要的是在哪一行整数上。

Example input:

示例输入:

1213 153 15 155
84 866 89 48
12
12 12 58
12

回答by David Rodríguez - dribeas

It depends on whether you want to do it in a line by line basis or as a full set. For the whole file into a vector of integers:

这取决于您是要逐行还是完整地进行。将整个文件转化为整数向量:

int main() {
   std::vector<int> v( std::istream_iterator<int>(std::cin), 
                       std::istream_iterator<int>() );
}

If you want to deal in a line per line basis:

如果您想在每行基础上处理一行:

int main()
{
   std::string line;
   std::vector< std::vector<int> > all_integers;
   while ( getline( std::cin, line ) ) {
      std::istringstream is( line );
      all_integers.push_back( 
            std::vector<int>( std::istream_iterator<int>(is),
                              std::istream_iterator<int>() ) );
   }
}

回答by synepis

You could do smtng like this(I used cin, but you can use any other file stream):

您可以像这样执行 smtng(我使用了 cin,但您可以使用任何其他文件流):

string line;
while( getline( cin, line ) )
{
 istringstream iss( line );
 int number;
 while( iss >> number )
  do_smtng_with_number();
}

Or:

或者:

int number;
while( cin >> number )
{
 do_smtng_with_number();
}

回答by Jerry Coffin

What result do you want? If you want all the integers in a single vector, you could do something like:

你想要什么结果?如果您想要单个向量中的所有整数,您可以执行以下操作:

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

std::vector<int> data(std::istream_iterator<int>(input),
                      std::istream_iterator<int>());

That discards the line-structure though -- you end up with the data all together. One easy way to maintain the original line structure is to read a line with getline, initialize a stringstream with that string, then put the values from that stringstream into a vector (and push that onto the back of a vector of vectors of int).

尽管如此,这丢弃了线结构——你最终得到了所有的数据。维护原始行结构的一种简单方法是使用 getline 读取一行,使用该字符串初始化 stringstream,然后将该 stringstream 中的值放入一个向量中(并将其推到 int 向量的向量的后面)。

std::vector<std::vector<int> > data;
std::vector<int> temp;

std::string t;
while (std::getline(input, t)) {
    std::istringstream in(t);
    std::copy(std::istream_iterator<int>(in), 
              std::istream_iterator<int>(), 
              std::back_inserter(temp);
    data.push_back(temp);
}

回答by Desai

Here you go :

干得好 :

void readFromFile(string filename)
{
    string line;
    ifstream myfile(filename);
    if (myfile.is_open())
    {
         while ( getline(myfile,line) )
    {
        cout << line << '\n';
    }
        myfile.close();
  }
}

int main(int argc, char* argv)
{
    readFromFile("Input.txt");
    getchar();
    return 0;
}