用C++读取一个TXT文件中的二维数组

我现在有一个txt文件,文件里面是一个二维数组,数组中的每行是用换行(回车)分开的的,每一行中的元素是用空格分开的。我现在想用C++读这个TXT文件,然后把其中的元素输出成一个vector数组,就是std::vector<std::vector<...>>这样的形式的。这个要怎么办呢?求助求助!!!!
举个例子啊:
-33 -36 25 387 -209...
22 9 987 -97 -88...
...
...
是一个几百乘几百的大的二维数组。

数据文件命名为data.txt,放置到应用程序同文件夹下。
---------------C++处理方法

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include<ctype.h>
#include <stdlib.h>
using namespace std;
inline void makeitem(vector<int>& item,istringstream &in );
int main(){
string line;
ifstream f("data.txt");
if(!f.is_open()){cout<<"error openfile"<<endl;return 1;}
vector<int> item;
vector<vector<int> > arr;
int num=0;
while(!f.eof()){
int num=0;
getline(f,line);
if(line.empty())continue;
istringstream input(line);
item.clear();
while(!input.eof()){
input>>num;
item.push_back(num);
}
arr.push_back(item);
}
f.close();
//output arr
for (int i=0;i<arr.size();i++){
item=arr[i];
for (int j=0;j<item.size();j++){
cout<<item[j]<<" ";
}
cout<<endl;
}
}

---------------c 字符串处理方法
#include <iostream>
#include <fstream>
#include <vector>
#include<ctype.h>
#include <stdlib.h>
using namespace std;
inline void makeitem(vector<int>& item,char *buf );
int main(){
char *buf=new char[1024];
ifstream f("data.txt");
if(!f.is_open()){cout<<"error openfile"<<endl;return 1;}
vector<int> item;
vector<vector<int> > arr;
int num=0;
while(!f.eof()){
f.getline(buf,1024);
makeitem(item,buf);
arr.push_back(item);
}
for (int i=0;i<arr.size();i++){
item=arr[i];
for (int j=0;j<item.size();j++){
cout<<item[j]<<" ";
}
cout<<endl;
}
}
inline char* nextnum(char* buf,char *p){
while(!isspace(*p)&&*p){
++p;
}
while(isspace(*p)){
++p;
}
return p;
}
inline void makeitem(vector<int>& item,char *buf ){
item.clear();
char *p=buf;
int num=0;
while(isspace(*p)){p++;}//跳过每行开始的空格。
while(*p){
num=atoi(p);//获取当前指针下方数字;
if(*p){item.push_back(num);}
p=nextnum(buf,p);//定位到下一个数字;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-03
#include <stdio.h>

#include <string.h>
#include <vector>
#include <iostream>
using namespace std;

void main()
{
vector< vector< int > > m;
FILE *fp = fopen( "C:\\1.txt", "rt" );
if ( !fp )
return;
char buf[30000+1];
while ( !feof( fp ) && fgets( buf, 30000, fp ) )
{
if ( buf[ strlen(buf) - 1] == 10 )
buf[ strlen(buf) - 1] = 0;
vector< int > v;
char* token = strtok( buf, " " );
while( token )
{
v.push_back( atoi( token ) );
token = strtok( NULL, " ");
}
if ( !v.empty() )
m.push_back( v );
}
fclose( fp );
}
第2个回答  2012-08-03
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
string filename;
vector< vector<int> > ivec;
int lineno = 0;
int numno = 0;
if (argc == 2) {
filename = argv[1];
} else if (argc > 2) {
cerr << "参数错误" << endl;
return EXIT_FAILURE;
} else {
cout << "输入文件名" << endl;
cin >> filename;
}
ifstream in(filename.c_str());
if (!in) {
cerr << "打开文件失败" << endl;
return EXIT_FAILURE;
}
string line;
stringstream sstm;
int val;
while (getline(in, line)) {
sstm << line;
vector<int> tvec;
while (sstm >> val) {
tvec.push_back(val);
++numno;
}
if (sstm.eof()) {
sstm.clear();
}
if (!sstm) {
cerr << "格式化数字错误" << endl;
return EXIT_FAILURE;
}
ivec.push_back(tvec);
++lineno;
}
if (in.eof()) {
in.clear();
}
if (!in) {
cerr << "读取文件错误" << endl;
return EXIT_FAILURE;
}
cout << "读取了 " << lineno << " 行, "
<< "共 " << numno << " 个数字" << endl;
return EXIT_SUCCESS;
}
第3个回答  2015-09-29
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;
int main(int argc, char *argv[]) {
string filename;
vector< vector<int> > ivec;
int lineno = 0;
int numno = 0;
if (argc == 2) {
filename = argv[1];
} else if (argc > 2) {
cerr << "参数错误" << endl;
return EXIT_FAILURE;
} else {
cout << "输入文件名" << endl;
cin >> filename;
}
ifstream in(filename.c_str());
if (!in) {
cerr << "打开文件失败" << endl;
return EXIT_FAILURE;
}
string line;
stringstream sstm;
int val;
while (getline(in, line)) {
sstm << line;
vector<int> tvec;
while (sstm >> val) {
tvec.push_back(val);
++numno;
}
if (sstm.eof()) {
sstm.clear();
}
if (!sstm) {
cerr << "格式化数字错误" << endl;
return EXIT_FAILURE;
}
ivec.push_back(tvec);
++lineno;
}
if (in.eof()) {
in.clear();
}
if (!in) {
cerr << "读取文件错误" << endl;
return EXIT_FAILURE;
}
cout << "读取了 " << lineno << " 行, "
<< "共 " << numno << " 个数字" << endl;
return EXIT_SUCCESS;
第4个回答  2012-08-03
逐行逐空格的atoi即可。