#include <fstream>
#include <iostream>
#include <string>
using namespace std;
//格式化读取
//起始点读取
void readformat_0(int* hour_0, int* minute_0, float* second_0, int tem[13],
float* pressure, ifstream & prefile)
{
char str1[15];
char str2[15];
char timeh[3];
char timem[3];
char times[7];
prefile >> str1 >> str2 >> tem[0] >> tem[1] >> tem[2] >> tem[3] >> *pressure >>tem[4]
>> tem[5] >>tem[6] >> tem[7] >> tem[8] >> tem[9] >> tem[10] >> tem[11] >> tem[12];
timeh[0] = str2[0]; timeh[1] = str2[1]; timeh[2] = '\0';
sscanf(timeh, "%d", hour_0);
timem[0] = str2[3]; timem[1] = str2[4]; timem[2] = '\0';
sscanf(timem, "%d", minute_0);
for(int i=0; i<6; i++)
{
times[i] = str2[6+i];
}
times[6] = '\0';
sscanf(times, "%f", second_0);
};
//其他时间读取,同时计算从初始开始的时间
void readformat(int hour_0, int minute_0, float second_0, float* time_tr,
int tem[13], float* pressure, ifstream & prefile)
{
char str1[15];
char str2[15];
char timeh[3];
char timem[3];
char times[7];
int hour;
int minute;
float second;
prefile >> str1 >> str2 >> tem[0] >> tem[1] >> tem[2] >> tem[3] >> *pressure >>tem[4]
>> tem[5] >>tem[6] >> tem[7] >> tem[8] >> tem[9] >> tem[10] >> tem[11] >> tem[12];
timeh[0] = str2[0]; timeh[1] = str2[1]; timeh[2] = '\0';
sscanf(timeh, "%d", &hour);
timem[0] = str2[3]; timem[1] = str2[4]; timem[2] = '\0';
sscanf(timem, "%d", &minute);
for(int i=0; i<6; i++)
{
times[i] = str2[6+i];
}
times[6] = '\0';
sscanf(times, "%f", &second);
*time_tr = second-second_0 + 60.0*(minute-minute_0) + 3600.0*(hour-hour_0);
};
//格式化输出
void excelformat(float time_tr, int tem[13], float pressure, ofstream & outfile)
{
outfile.width(10);outfile.precision(3);outfile.setf(ios::fixed);
outfile << time_tr ;
for (int i = 0; i < 4; i++)
{
outfile.width(4); outfile << tem[i];
}
outfile.width(7); outfile.precision(3);outfile.setf(ios::fixed);
outfile << pressure;
for (int i = 4; i < 11; i++)
{
outfile.width(4); outfile <<tem[i];
}
outfile.width(7); outfile << tem[11];
outfile.width(4); outfile << tem[12];
outfile << endl;
};
int main()
{
//打开文件进行读取
ifstream prefile;
ofstream outfile;
prefile.open("test.txt",ios::in);
outfile.open("test_new.txt");
if(!prefile.is_open())
{
cout << "failed open" << endl;
}
cout << "Reading from the file" << endl;
//读取前两行的表头信息,整行读取
int tem[13];
float pressure;
int hour_0;
int minute_0;
float second_0;
float time_tr = 0.0;
//读取表格信息
//读取第一行,为时间零点,输出到新文件
readformat_0(&hour_0, &minute_0, &second_0, tem, &pressure, prefile);
excelformat(time_tr, tem, pressure, outfile);
//读取其他行,得到对应的时间,输出到新文件
while (!prefile.eof())
{
readformat(hour_0, minute_0, second_0, &time_tr, tem, &pressure, prefile);
excelformat(time_tr, tem, pressure, outfile);
}
cout << "transfer completed!" <<endl;
prefile.close();
outfile.close();
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
C++格式化读写文件操作
共1个文件
cpp:1个
需积分: 0 1 下载量 37 浏览量
2023-07-27
17:01:16
上传
评论
收藏 1KB ZIP 举报
温馨提示
C++中的文件读写可以使用标准库中的iostream库来完成。下面是描述如何进行格式化的文件读写的步骤,不包含具体的代码实现: 包含头文件:在程序中包含iostream头文件,以便使用输入输出流。 打开文件:使用fstream类中的open()函数打开需要读写的文件。可以指定打开方式为读取模式(ifstream)或写入模式(ofstream)。 格式化读取:使用输入流对象(例如cin或ifstream对象)和格式控制符,按照特定的格式从文件中读取数据。可以使用>>运算符将数据读取到变量中。 格式化写入:使用输出流对象(例如cout或ofstream对象)和格式控制符,按照特定的格式将数据写入文件中。可以使用<<运算符将数据写入文件。 关闭文件:使用fstream类中的close()函数关闭文件,释放资源。 在格式化读写文件时,可以使用各种格式控制符来指定读取或写入数据的格式,例如%d(整数)、%f(浮点数)、%s(字符串)等。还可以使用setw()、setprecision()等函数来设置字段宽度和精度。 这些步骤描述了如何使用C++进行格式化的文件读写操作,具体实现可以
资源推荐
资源详情
资源评论
收起资源包目录
C++格式化读写.zip (1个子文件)
C++格式化读写
test.cpp 3KB
共 1 条
- 1
资源评论
童小纯
- 粉丝: 3w+
- 资源: 289
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功