首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

读取这种数据该怎么设置

2012-03-30 
读取这种数据该如何设置在txt文件中有这样的数据-7.89637992E+01,6.36982546E+01,-5.93000854E+00,1P2-7.8

读取这种数据该如何设置
在txt文件中有这样的数据
-7.89637992E+01,6.36982546E+01,-5.93000854E+00,                                                 1P             2
-7.87123487E+01,6.42194476E+01,-6.01360995E+00,                                                 2P             3
-7.84347478E+01,6.47294635E+01,-6.06792136E+00,                                                 3P             4
-7.81045753E+01,6.51780915E+01,-6.08370287E+00,                                                 4P             5
-7.77535095E+01,6.55868160E+01,-6.09925731E+00,                                                 5P             6
-7.73861871E+01,6.59952363E+01,-6.15497259E+00,                                                 6P             7
-7.70403322E+01,6.64051253E+01,-6.15643250E+00,                                                 7P             8
-7.66741398E+01,6.67753405E+01,-6.10056749E+00,                                                 8P             9
-7.63083584E+01,6.71506491E+01,-6.04024156E+00,                                                 9P           10
-7.59232257E+01,6.75241155E+01,-5.99577062E+00,                                                 10P           11
-7.55245609E+01,6.78264308E+01,-5.90354463E+00,                                                 11P           12
-7.51568280E+01,6.82087417E+01,-5.86449557E+00,                                                 12P           13


我想只读取前面的数据,空格以后的全部不要
该如何读取呢,是不是碰到空格就换行?
我不知道哪些函数,大家给说一说,谢谢

[解决办法]
#include <sstream>
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>

using namespace std;

struct points
{
double x,y,z;
};


int main(int argc, char **argv)


{
struct points t;
string line;
ifstream ifile( "test.txt ");

while(!ifile.eof())
{
getline(ifile, line);
cout < <line;
sscanf(line.c_str(), "%lf,%lf,%lf ", &t.x, &t.y, &t.z);
cout < <t.x < <endl
< <t.y < <endl
< <t.z < <endl < <endl;
}

system( "pause ");
return 0;
}

test.txt 文件内容:(在工程当前目录下)
-7.89637992E+01,6.36982546E+01,-5.93000854E+00, 1P 2
-7.87123487E+01,6.42194476E+01,-6.01360995E+00, 2P 3
-7.84347478E+01,6.47294635E+01,-6.06792136E+00, 3P 4
-7.81045753E+01,6.51780915E+01,-6.08370287E+00, 4P 5
-7.77535095E+01,6.55868160E+01,-6.09925731E+00, 5P 6
-7.73861871E+01,6.59952363E+01,-6.15497259E+00, 6P 7
-7.70403322E+01,6.64051253E+01,-6.15643250E+00, 7P 8
-7.66741398E+01,6.67753405E+01,-6.10056749E+00, 8P 9
-7.63083584E+01,6.71506491E+01,-6.04024156E+00, 9P 10
-7.59232257E+01,6.75241155E+01,-5.99577062E+00, 10P 11
-7.55245609E+01,6.78264308E+01,-5.90354463E+00, 11P 12
-7.51568280E+01,6.82087417E+01,-5.86449557E+00, 12P 13

热点排行