C++中txt的文件用二维数组读取(yj)

来源:百度知道 编辑:UC知道 时间:2024/05/20 15:10:09
有如下的txt数据:(10行4列,第一列为ID号,第二列为x坐标,第二列y坐标,第三列z坐标),如何在C++中将txt文件中的这些数据按照2维数组提取出来显示呢
1,10107.5,10340.2,331.46
2,10110.4,10360.1,331.45
3,10114.4,10377.5,332.67
4,10119.4,10392.4,332.76
5,10122.8,10409.6,333.87
6,10150.4,10309.9,329.23
7,10127.5,10421.5,335.26
8,10160.6,10329.7,337.65
9,10143.4,10445.9,338.36
10,10131.9,10409.2,333.69

//---------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;
int main(void)
{
ifstream a("yj.txt");////数据文件名为yj.txt

string line;
int i=0,t1;
char t2;
float xy[10][3];
while (getline(a,line))
{

istringstream astr(line);
astr>>t1>>t2>>xy[i][0]>>t2>>xy[i][1]>>t2>>xy[i][2];
++i;
}
a.close();
for (i = 0; i<10; i++) {
cout<<i+1<<"\t:\t"<<xy[i][0]<<"\t\t"<<xy[i][1]<<"\t\t"<<xy[i][2]<<endl;
}

return 0;
}
//---------------------------------------------------------------------------

c语言可以存入以逗号隔开的数据,c++我只知道可以存入以空格隔开的数据,你把数据用空格隔