test2.txt
6 99 9 2 17 51 44.0 -.839701388031D-03 -.165982783074D-10 .000000000000D+00
.910000000000D+02 .934062500000D+02 .116040547840D-08 .162092304801D+00
.484101474285D-05 .626740418375D-02 .652112066746D-05 .515365489006D+04
.409904000000D+06 -.242143869400D-07 .329237003460D+00 -.596046447754D-07
.111541663136D+01 .326593750000D+03 .206958726335D+01 -.638312302555D-08
.307155651409D-09 .000000000000D+00 .102500000000D+04 .000000000000D+00
.000000000000D+00 .000000000000D+00 .000000000000D+00 .910000000000D+02
.406800000000D+06 .000000000000D+00
6 99 9 2 17 51 44.0-0.839701388031D-03-0.165982783074D-10 0.000000000000D+00
0.910000000000D+02 0.934062500000D+02 0.116040547840D-08 0.162092304801D+00
0.484101474285D-05 0.626740418375D-02 0.652112066746D-05 0.515365489006D+04
0.409904000000D+06-0.242143869400D-07 0.329237003460D+00-0.596046447754D-07
0.111541663136D+01 0.326593750000D+03 0.206958726335D+01-0.638312302555D-08
0.307155651409D-09 0.000000000000D+00 0.102500000000D+04 0.000000000000D+00
0.000000000000D+00 0.000000000000D+00 0.000000000000D+00 0.910000000000D+02
0.406800000000D+06 0.000000000000D+00
c++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(void){
ifstream fin;
// open file
fin.open("test2.txt", ios::in);
// if file is open
if(fin.is_open()){
// ephemeris data
int intPrn;
int intYear, intMonth, intDay, intHr, intMin;
float numSec;
double numData[4];
// count line (must start from -1)
int intLine = -1;
// if file can read
while(!fin.eof()) {
// count++
intLine = (intLine<(8-1)) ? intLine+1 : 0;
// line information
string strLine;
getline(fin, strLine);
// save 4 string
string strA, strB, strC, strD;
strA = (strLine.size() > 0) ? strLine.substr(0,22) : "";
strB = (strLine.size() > 22) ? strLine.substr(22,19) : "";
strC = (strLine.size() > 41) ? strLine.substr(41,19) : "";
strD = (strLine.size() > 60) ? strLine.substr(60,99) : "";
if(intLine == 0){
// use sscanf read prn and time tag
sscanf(strA.c_str(), "%d %d %d %d %d %d %f", &intPrn, &intYear, &intMonth, &intDay, &intHr, &intMin, &numSec);
//cout << intPrn << endl;
//cout << "yy dd mm HH MM SS: " << intYear << " " << intMonth << " " << intDay << " " << intHr << " " << intMin << " " << numSec << endl;
// tag line, save 3 data (from 1~3)
numData[1] = strtod(strB.c_str(), NULL);
numData[2] = strtod(strC.c_str(), NULL);
numData[3] = strtod(strD.c_str(), NULL);
// create char to save char
char *chrLine;
chrLine = new char[80+1];
sprintf(chrLine, "%2d %2d %2d %2d %2d %2d %4.1f%+19.12E%+19.12E%+19.12E\n",
intPrn, intYear, intMonth, intDay, intHr, intMin, numSec,
numData[1], numData[2], numData[3]);
// build string from char
string strOut = chrLine;
strOut.erase(79,1).erase(59,1).erase(39,1);
cout << strOut << endl;
}else{
// not tag line, save 4 data (from 0~3)
numData[0] = strtod(strA.c_str(), NULL);
numData[1] = strtod(strB.c_str(), NULL);
numData[2] = strtod(strC.c_str(), NULL);
numData[3] = strtod(strD.c_str(), NULL);
//cout << strLine.c_str()+3 << endl;
// create char to save char
char *chrLine;
chrLine = new char[80+1];
sprintf(chrLine, "%+19.12E%+19.12E%+19.12E%+19.12E\n", numData[0], numData[1], numData[2], numData[3]);
// build string from char
string strOut = chrLine;
// force cut E+000 to E+00
strOut.erase(78,1).erase(58,1).erase(38,1).erase(18,1);
cout << " " << strOut << endl;
}
}
}
fin.close();
system("PAUSE");
return 0;
}
沒有留言:
張貼留言