C++中txt的文件用二维数组读取(yj)
有如下的txt数据:(10行4列,第一列为ID号,第二列为x坐标,第二列y坐标,第三列z坐标),如何在C++中将txt文件中的这些数据按照2维数组提取出来显示呢1,101...
有如下的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 展开
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 展开
2个回答
展开全部
c语言可以存入以逗号隔开的数据,c++我只知道可以存入以空格隔开的数据,你把数据用空格隔开才可以用下面的程序编译。
#include<fstream>
#include<iostream>
using namespace std;
struct list
{
int num;
float x_coor;
float y_coor;
float z_coor;
};
int main()
{
int n = 0,i=0,count=0;
list a[10];
ifstream fin("hello.txt");
if(!fin)
{
cout<<"open failed!"<<endl;
exit(1);
}
while(fin>>a[i].num>>a[i].x_coor>>a[i].y_coor>>a[i].z_coor)
{
//cout<<a[i].num<<" "<<a[i].x_coor<<" "<<a[i].y_coor<<" "<<a[i].z_coor<<endl;
i++;
count++;
}
fin.close();
for(i=0;i<count;i++)
cout<<a[i].num<<" "<<a[i].x_coor<<" "<<a[i].y_coor<<" "<<a[i].z_coor<<endl;
return 0;
}
#include<fstream>
#include<iostream>
using namespace std;
struct list
{
int num;
float x_coor;
float y_coor;
float z_coor;
};
int main()
{
int n = 0,i=0,count=0;
list a[10];
ifstream fin("hello.txt");
if(!fin)
{
cout<<"open failed!"<<endl;
exit(1);
}
while(fin>>a[i].num>>a[i].x_coor>>a[i].y_coor>>a[i].z_coor)
{
//cout<<a[i].num<<" "<<a[i].x_coor<<" "<<a[i].y_coor<<" "<<a[i].z_coor<<endl;
i++;
count++;
}
fin.close();
for(i=0;i<count;i++)
cout<<a[i].num<<" "<<a[i].x_coor<<" "<<a[i].y_coor<<" "<<a[i].z_coor<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-03-29
展开全部
//---------------------------------------------------------------------------
#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;
}
//---------------------------------------------------------------------------
#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;
}
//---------------------------------------------------------------------------
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询