📄 readofile.cpp
字号:
#include"myStruct.h"
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
void readOfile(vector<epoch_set>& v,double& apx,double& apy,double& apz )
{
time_gps GregToGps(time_calendar tc);//声明时间转换函数
char *p, name[20];
p=name;
cout<<"Enter O file name:";
scanf("%s",p);
ifstream infile(p,ios::in);
if(! infile)
{
cerr<<"open error!"<<endl;
exit(1);//打开文件失败
}
////////////////////////////////////////////////////////
string str,str1,str3;//not good declared,暂时轮换存放取得的字符
int pos,pos1,pos2,pos3,pos_CA,pos_L1,pos_L2;//CA/L1/L2码位置
int obsNum,n=0,e=0;//观测值类型总数,文件阅读的行数
ofstream outfile("历元对应行数.txt",ios::out);
//这里的do开始读头文件---------------------------------------------------------------
do
{
getline(infile,str);
pos=str.find("C1",0);//在读的过程顺便找C1所在的位置
pos1=str.find("L1",0);//L1所在的位置
pos2=str.find("L2",0);//L2所在的位置
pos3=str.find("APPROX",0);//近似坐标所在位置
if(pos3!=-1)//如果找到APPROX,获取近似坐标
{ //success return pos,else return npos=-1;
str1=str.substr(0,14);apx=atof(str1.c_str());
str1=str.substr(15,28);apy=atof(str1.c_str());
str1=str.substr(29,42);apz=atof(str1.c_str());
}
if (pos!=-1) //如果找到C1
{
str3=str.substr(0,6);//从第0个字符开始读6个字符,取得观测值类型总数
obsNum=atoi(str3.c_str());//字符转换为整数
pos_CA=(pos-10)/6;//位置从0,1,2,3...
pos_L1=(pos1-10)/6;
pos_L2=(pos2-10)/6;
}
n++;
} while(str.substr(60,3)!="END");//这个do是读到文件头结束END OF HEAD的地方停止
//cout<<"-------------o file header line is--"<<n<<"--hang------------"<<endl;
//cycle------------an li yuan da cycle---vector???
epoch_set es;//定义结构:一个历元的所有数据
while(!infile.eof())//当不是文件末尾时
{
//读一个历元的数据
epoch_HedMesg eh1;//开始读历元头:历元,历元标志,卫星总数,PRN列表(String),接收机偏差
time_calendar tc1;//读取日历时间后再转换,现在还没转
getline(infile,str1);//-------------------读取了一行
string str2=str1.substr(0,3);//提取3个字符--year
tc1.year=atoi(str2.c_str());//字符转数值
str2=str1.substr(3,3);//提取--month
tc1.month=atoi(str2.c_str());
str2=str1.substr(6,3);//提取--day
tc1.day=atoi(str2.c_str());
str2=str1.substr(9,3);//提取--hour
tc1.hour=atoi(str2.c_str());
str2=str1.substr(12,3);//提取--minute
tc1.minute=atoi(str2.c_str());
str2=str1.substr(15,3);//提取--second
tc1.second=atoi(str2.c_str());
str2=str1.substr(27,3);//提取--flag历元标志:0正常,1电源故障...
eh1.flag=atoi(str2.c_str());
time_gps tg1;
tg1=GregToGps(tc1);
es.gps_sat=tg1;//读取了观测历元时刻
str2=str1.substr(30,3);//提取目前历元--satNum卫星总数
eh1.satNum=atoi(str2.c_str());//两个地方都用
es.num_sat=atoi(str2.c_str());
int widthnum=eh1.satNum*3;//一颗卫星3个字符,卫星数*3=字符宽度
str2=str1.substr(33,widthnum);//提取--satList
eh1.satList=str2;//list还只是一个字符串
es.list_sat=str2;
n++;e++;
outfile<<"历元"<<e<<"=>"<<n<<endl;
//接收机钟差没读
//--------------------------历元头数据读完---------------
//----------------------- 开始读观测值数据--------------
complex_obs cplxobs_temp;//暂时存放
my_obs mobs;
int k=0;
for(int j=0;j<eh1.satNum;j++)//从0到卫星数<8=0:7
{
string tempstr;//存放2次读的字符
if(obsNum>5)//如果观测值数大于5,要占两行
{
getline(infile,str1);//一次有2行
getline(infile,str);
tempstr=str1+str;//一颗卫星的数据放在了一行
n+=2;//计算读了多少行,一共3个地方
}
else {getline(infile,tempstr); n++;}
//cout<<"------------last str="<<tempstr<<endl;
//下面根据C1 L1 L2的存放顺序读取
str2=tempstr.substr(pos_CA*16,14);//提取--pseudo_obs只有观测值,暂无LLi,intension
cplxobs_temp.pseudo_obs.value=atof(str2.c_str());
if(cplxobs_temp.pseudo_obs.value==0) mobs.flag="C1";//判断观测值的缺失
str2=tempstr.substr(pos_L1*16,14);//提取--phrase1
cplxobs_temp.phrase1_obs.value=atof(str2.c_str());
if (cplxobs_temp.phrase1_obs.value==0) mobs.flag="L1";//判断观测值的缺失
str2=tempstr.substr(pos_L2*16,14);//提取--phrase2
cplxobs_temp.phrase2_obs.value=atof(str2.c_str());
if (cplxobs_temp.phrase2_obs.value==0) mobs.flag="L2";//判断观测值的缺失
//...................先提取这3种C1 L1 L2观测值
string listtemp=eh1.satList.substr(k,2);//从头里面,k用来表示步长
mobs.sat_num=atof(listtemp.c_str());//提取--对应卫星号
k+=3;//3个字符递增0,3,6,9...
mobs.set_obs=cplxobs_temp;//above give to my_obs
mobs.tgps=GregToGps(tc1);//??????????????????????????
es.array_sat[j]=mobs;//give above j=0,1,2,3.... <24
}
v.push_back(es);//根据卫星的个数每读一次,推进一次。防范空行????if flag=2??
}
cout<<"文件行数n="<<n<<"历元数e="<<e<<endl;
infile.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -