请教C++程序中的ifstream infile
#include"myStruct.h"
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
void readNfile(vector<nav_sat>& vn)
{
time_gps GregToGps(time_calendar tc);//声明时间转换
char *p, name[20];
p=name;
cout<<"Enter N file name:";
scanf("%s",p);
ifstream infile1(p,ios::in);
if(! infile1)
{
cerr<<"open error!"<<endl;
exit(1);//打开文件失败
}
////////////////////////////////////////////////////////
string strs;//not good declared,暂时轮换存放取得的字符
do
{
getline(infile1,strs);//没有读文件头
} while(strs.substr(60,3)!="END");
//go on reading...
while(!infile1.eof())//当不是文件末尾的时候,一直读
{
pare_s_clock paresclk;//卫星钟差三参数&TOC
time_calendar tc1;//TOC
time_gps tg1;
getline(infile1,strs);//读一行
nav_sat navsat;//定义结构:代表一颗卫星的导航数据
string str2=strs.substr(2,3);//提取3个字符--year
tc1.year=atoi(str2.c_str());//字符转数值
str2=strs.substr(5,3);//提取--month
tc1.month=atoi(str2.c_str());
str2=strs.substr(8,3);//提取--day
tc1.day=atoi(str2.c_str());
str2=strs.substr(11,3);//提取--hour
tc1.hour=atoi(str2.c_str());
str2=strs.substr(14,3);//提取--minute
tc1.minute=atoi(str2.c_str());
str2=strs.substr(17,5);//提取--second
tc1.second=atoi(str2.c_str());
tg1=GregToGps(tc1);//转换为GPS时间
paresclk.TOC.num_week=tg1.num_week ;
paresclk.TOC.num_sec=tg1.num_sec;
str2=strs.substr(22,19);//卫星钟的偏差
paresclk.a0=atof(str2.c_str());
str2=strs.substr(41,19);//卫星钟的漂移
paresclk.a1=atof(str2.c_str());
str2=strs.substr(60,19);//卫星钟的漂移速度
paresclk.a2=atof(str2.c_str());
str2=strs.substr(0,2);//提取--卫星号
navsat.PRN=atoi(str2.c_str());
navsat.pare_clock=paresclk;//单个卫星数据组第一行读完
不管p指针输入什么,程序一直会输出"open error!"
#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<iomanip>
using namespace std;
void readNfile(vector<nav_sat>& vn)
{
time_gps GregToGps(time_calendar tc);//声明时间转换
char *p, name[20];
p=name;
cout<<"Enter N file name:";
scanf("%s",p);
ifstream infile1(p,ios::in);
if(! infile1)
{
cerr<<"open error!"<<endl;
exit(1);//打开文件失败
}
////////////////////////////////////////////////////////
string strs;//not good declared,暂时轮换存放取得的字符
do
{
getline(infile1,strs);//没有读文件头
} while(strs.substr(60,3)!="END");
//go on reading...
while(!infile1.eof())//当不是文件末尾的时候,一直读
{
pare_s_clock paresclk;//卫星钟差三参数&TOC
time_calendar tc1;//TOC
time_gps tg1;
getline(infile1,strs);//读一行
nav_sat navsat;//定义结构:代表一颗卫星的导航数据
string str2=strs.substr(2,3);//提取3个字符--year
tc1.year=atoi(str2.c_str());//字符转数值
str2=strs.substr(5,3);//提取--month
tc1.month=atoi(str2.c_str());
str2=strs.substr(8,3);//提取--day
tc1.day=atoi(str2.c_str());
str2=strs.substr(11,3);//提取--hour
tc1.hour=atoi(str2.c_str());
str2=strs.substr(14,3);//提取--minute
tc1.minute=atoi(str2.c_str());
str2=strs.substr(17,5);//提取--second
tc1.second=atoi(str2.c_str());
tg1=GregToGps(tc1);//转换为GPS时间
paresclk.TOC.num_week=tg1.num_week ;
paresclk.TOC.num_sec=tg1.num_sec;
str2=strs.substr(22,19);//卫星钟的偏差
paresclk.a0=atof(str2.c_str());
str2=strs.substr(41,19);//卫星钟的漂移
paresclk.a1=atof(str2.c_str());
str2=strs.substr(60,19);//卫星钟的漂移速度
paresclk.a2=atof(str2.c_str());
str2=strs.substr(0,2);//提取--卫星号
navsat.PRN=atoi(str2.c_str());
navsat.pare_clock=paresclk;//单个卫星数据组第一行读完
不管p指针输入什么,程序一直会输出"open error!"
...全文
相关推荐
Learning-cPP-array-of-objects.zip_文件操作_Visual_C++_
通过分析和理解这个源代码,开发者能够掌握在Visual C++环境下处理对象数组以及文件操作的基本技能,这对于任何C++程序员来说都是至关重要的。
面向对象C++填空题.docx
C++程序的源文件通常使用.cpp为扩展名。20. include命令可以包含系统定义的头文件,也可以包含自定义的头文件,自定义头文件通常使用" "包含。21.
ofstream,ifstream,fstream读写文件
ofstream, ifstream, 和 fstream 是C++标准库中的三个核心文件流类,它们属于 `<fstream>` 头文件,用于实现对磁盘文件的读写操作。
ofstream ifstream 文件操作
### 标题解释:`ofstream ifstream 文件操作`标题提到了 `ofstream` 和 `ifstream` 的基本用法,这在Visual C++ (VC) 环境中非常常见。
cpp代码-案例1 老师学生案例
综上所述,这个“cpp代码-案例1 老师学生案例”涉及了C++中的面向对象编程(类和对象的定义、构造函数、成员函数),文件I/O操作,以及可能的数据序列化与反序列化技术。


