读取整数文件并将EACH行存储在一个单独的数组中

时间:2022-05-08 22:36:46

I'm trying to read this file and store each row in one array. Can someone please tell me how to implement the code for this?

我正在尝试读取此文件并将每行存储在一个数组中。有人可以告诉我如何实现这个代码?

2
2 10 1 2 7
3 8 3 7 7 10 7

I have the code below and I can save every element of the text file into an array, but I need to have the lines saved into seperate arrays. How do I do this?

我有下面的代码,我可以将文本文件的每个元素保存到一个数组中,但我需要将这些行保存到单独的数组中。我该怎么做呢?

fstream myfile("myfile's_address", ios_base::in);

int a;
while (myfile >> a)
{
    word[increment] = a;
    increment++;
}

1 个解决方案

#1


3  

First, use ifstream instead of fstream with ios_base::in.

首先,使用ifstream代替fstream和ios_base :: in。

Next, use std::getline() to get one line as a string, create one vector for it (perhaps in a vector<vector<string>>), then parse it (perhaps using std::istringstream).

接下来,使用std :: getline()将一行作为字符串,为它创建一个向量(可能在向量 >中),然后解析它(可能使用std :: istringstream)。

#1


3  

First, use ifstream instead of fstream with ios_base::in.

首先,使用ifstream代替fstream和ios_base :: in。

Next, use std::getline() to get one line as a string, create one vector for it (perhaps in a vector<vector<string>>), then parse it (perhaps using std::istringstream).

接下来,使用std :: getline()将一行作为字符串,为它创建一个向量(可能在向量 >中),然后解析它(可能使用std :: istringstream)。