使用C ++将不规则文件读入2维数组 [英] read irregular file into a 2-dimension array using C++
本文介绍了使用C ++将不规则文件读入2维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是C ++的新手。我相信在stackoverflow已经有解决方案,但我找不到任何。
I am very new to C++. I believe there are solutions already in stackoverflow but I cannot find any.
我需要从一个txt文件读取数据到一个2维数组。文件类似于
I need to read data from a txt file into a 2 dimension array. File is like
54 3 5 678
54 3 5 678
10 1 2 3 46 8 1 1 2 3 4
10 1 2 3 46 8 1 1 2 3 4
9 8 10
到120个整数,并且不超过60行。
Each line contains up to 120 integers and there are no more than 60 lines.
您的回复非常感谢。谢谢!
Your reply is high appreciated. Thanks!
更新:不是家庭作业。
推荐答案
我的解决方案,只需用您的文件流替换cin
My solution, just substitute cin with your file stream
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(int argc, char* argv[]) {
string line;
int ints[60][120] = {0}, i = 0;
stringstream ss;
while (getline(cin, line)) {
ss.str(line);
for(int j = 0; j < 120 && ss; j++) {
ss >> ints[i][j];
}
ss.clear();
i++;
}
cin.ignore();
return 0;
}
这篇关于使用C ++将不规则文件读入2维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文