如何读取以下文件啊,谢谢,急
2007-07-18 21:20:003.6482.0353.315448.980293.850484.750
2007-07-18 21:25:003.6482.0353.315448.980293.850484.750
2007-07-18 21:30:003.6482.0353.315448.980293.850484.750
2007-07-18 21:35:003.6482.0353.315448.980293.850484.750
2007-07-18 21:40:003.6482.0353.315448.980293.850484.750
2007-07-18 21:45:003.6482.0353.315448.980293.850484.750
2007-07-18 21:50:003.6482.0353.315448.980293.850484.750
2007-07-18 21:55:003.6482.0353.315448.980293.850484.750
2007-07-18 22:00:003.6482.0353.315448.980293.850484.750
说明:
采集日期[SPACE]采集时间[TAB]组分1浓度[TAB]组分2浓度[TAB]组分3浓度[TAB]组分4浓度[TAB][ENTER]
用C++读取,格式为TXT
谢谢
[解决办法]
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
string line;
string data, time;
float c1, c2, c3, c4, c5, c6;
ifstream ifile( "test.txt ");
while (!ifile.fail())
{
getline(ifile, line);
istringstream tmp(line);
tmp> > data> > time> > c1> > c2> > c3> > c4> > c5> > c6;
cout < < "采集日期 = " < <data < <endl
< < "采集时间 = " < <time < <endl
< < "组分1浓度 = " < <c1 < <endl
< < "组分2浓度 = " < <c2 < <endl
< < "组分3浓度 = " < <c3 < <endl
< < "组分4浓度 = " < <c4 < <endl
< < "组分5浓度 = " < <c5 < <endl
< < "组分6浓度 = " < <c6 < <endl
< <endl;
system( "PAUSE ");
}
ifile.close();
return 0;
}
test.txt 在当前目录下,内容为:
2007-07-18 21:20:003.6482.0353.315448.980293.850484.750
2007-07-18 21:25:003.6482.0353.315448.980293.850484.750
2007-07-18 21:30:003.6482.0353.315448.980293.850484.750
2007-07-18 21:35:003.6482.0353.315448.980293.850484.750
2007-07-18 21:40:003.6482.0353.315448.980293.850484.750
2007-07-18 21:45:003.6482.0353.315448.980293.850484.750
2007-07-18 21:50:003.6482.0353.315448.980293.850484.750
2007-07-18 21:55:003.6482.0353.315448.980293.850484.750
2007-07-18 22:00:003.6482.0353.315448.980293.850484.750
[解决办法]
更简单点:
int main()
{
string data, time;
float c1, c2, c3, c4, c5, c6;
ifstream ifile( "test.txt ");
while (!ifile.fail())
{
ifile> > data> > time> > c1> > c2> > c3> > c4> > c5> > c6;
cout < < "采集日期 = " < <data < <endl
< < "采集时间 = " < <time < <endl
< < "组分1浓度 = " < <c1 < <endl
< < "组分2浓度 = " < <c2 < <endl
< < "组分3浓度 = " < <c3 < <endl
< < "组分4浓度 = " < <c4 < <endl
< < "组分5浓度 = " < <c5 < <endl
< < "组分6浓度 = " < <c6 < <endl
< <endl;
system( "PAUSE ");
}
ifile.close();
return 0;
}
------解决方案--------------------
呵呵,那我来个复杂点的- -
//存放数据的结构体
typedef struct stuTest
{
string Date;
string Time;
double Group1;
double Group2;
double Group3;
double Group4;
double Group5;
double Group6;
}Test,*pTest;
//字符串流输入操作符重载
istringstream& operator> > (istringstream& istrstream,pTest &test)
{
istrstream > > test-> Date
> > test-> Time
> > test-> Group1
> > test-> Group2
> > test-> Group3
> > test-> Group4
> > test-> Group5
> > test-> Group6;
return istrstream;
}
//输出操作符重载
ostream& operator < <(ostream& os,pTest test)
{
os < < test-> Date < < "\t "
< < test-> Time < < "\t "
< < test-> Group1 < < "\t "
< < test-> Group2 < < "\t "
< < test-> Group3 < < "\t "
< < test-> Group4 < < "\t "
< < test-> Group5 < < "\t "
< < test-> Group6 < < "\t ";
return os;
}
int main()
{
ifstream ifs( "1.txt ");//打开文件流,1.txt为存放数据的文件名
vector <pTest> stutestVec;//存放结构体指针的容器
vector <pTest> ::iterator stutestVecIt;
string str;
while (!ifs.eof())
{
getline(ifs,str);//从文件流中读取一行到string str中
istringstream istrstream(str);
pTest test = new Test;
istrstream > > test;//从字符串留中读取数据到结构体
stutestVec.push_back(test);//保存结构体指针
}
stutestVecIt = stutestVec.begin();
//输出
while (stutestVecIt != stutestVec.end())
{
cout < < *stutestVecIt < < endl;
delete *stutestVecIt;
stutestVecIt++;
}
return 0;
}