0

这是示例文本文件内容:

5 //列

Id,Age,history,chemistry,biology //列名

100//数据行数

3245167,12,45,78,12 //用逗号分隔的数据行

30980424,10,26,38,98

等等..

这是我到目前为止的一般代码:

int main()
{
  //prints out input from file.

  ifstream myFile;
  myFile.open("WRITE FILE NAME");

  while(Myfile.good()) { // good means while there is smthg in the file keep reading it
    // until you reach to the end.

    string line;

    getline(myFile, line, ','); //read text until comma, then stop and store in variable.
    cout << line << endl;
  }
  return 0;
}
4

1 回答 1

0

你有一个解析文件本身的大致轮廓,数据将从左到右读取文件。因此,由于您已经解析了数据,因此我建议您在这一点上做两件事。您可能需要一些东西来保存它,比如一个队列来存储和保存所有数据,以及一个双 for 循环来将它放入 2D 数组中,如下所示:

std::queue<string> holder;
std::string myArray[row][col];

getline(myFile, line, ',');
holder.push(line);

for(int i=0; i < row; i++)
{ 
    for(int j=0; j < col; j++)
    {
      myArray[i][j] = holder.pop();
    }
 }
于 2020-09-24T03:34:03.007 回答