当前位置:网站首页>ofstream,ifstream,fstream读写文件
ofstream,ifstream,fstream读写文件
2022-08-02 01:01:00 【妙为】
系列文章目录
前言
在编程总经常会用到读写文件,基本都是使用ofstream,ifstream,fstream
一、ofstream
#include <fstream>
ofstream //文件写操作 内存写入存储设备
ifstream //文件读操作,存储设备读区到内存中
fstream //读写操作,对打开的文件可进行读写操作
在ofstream类中,成员函数open()实现打开文件的操作,从而将数据流和文件进行关联。下面这段代码依次打开0~9.txt 10个文件
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<map>
#include<omp.h>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#include<gdiplus.h>
using namespace std;
int main(int argc, char** argv)
{
for (int i=0; i<10; i++)
{
ofstream outFile;
std::string fileName = to_string(i) + ".txt";
outFile.open(fileName, std::ios::out);
double temp = 0.0;
outFile << std::to_string(temp) << " "
<< std::to_string(temp) << " "
<< std::to_string(temp) << "\n";
outFile.close();
}
return 0;
}
每个文件输出内容
注意:如果代码写成下面这样:
也就是说:ofstream初始化的时候,不要指定文件,在调用open函数的时候再指定文件名,否则文件中的内容是空的,切记切记。我当时用了一个上午才发现问题,血淋淋的教训啊!!!!
还有就是写完文件记得关闭文件呦
二、ifstream
ifstream:读文件,用法跟ofstream差不多
ifstream fin;
double data;
fin.open(getFilePath() + "\\model\\20210408xiugai.txt");
int k = 0;
while (1)
{
if (fin.eof())
{
break;
}
else
{
fin >> data;
fin >> data;
weather1.vec_AirT.push_back(data);
fin >> data;
weather1.vec_Rh.push_back(data);
fin >> data;
weather1.vec_WindvSpeed.push_back(data);
fin >> data;
weather1.vec_Solar.push_back(data);
//cout << k << endl;
k += 1;
}
}
fin.close();
三、fstream
fstream:可读可写数据,fstream 类为所有内建数据类型以及 std::string 和 std::complex 类型重载 << 和 >> 操作符。下面的例子示范了这些操作符的使用方法:
#include<Windows.h>
#include<iostream>
#include<fstream>
#include<map>
#include<omp.h>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#include<gdiplus.h>
using namespace std;
int main(int argc, char** argv)
{
//fstream写数据
for (int i=0; i<10; i++)
{
std::string fileName = to_string(i) + ".txt";
fstream inOutFile;
inOutFile.open(fileName, std::ios::out | std::ios::in | std::ios::trunc);
double temp = i + 10;
inOutFile << std::to_string(temp) << " "
<< std::to_string(temp) << " "
<< std::to_string(temp) << "\n";
inOutFile.close();
}
//fstream读数据
for (int i=0; i<10; i++)
{
std::string fileName = to_string(i) + ".txt";
fstream inOutFile;
inOutFile.open(fileName, std::ios::out | std::ios::in);
double temp = 0;
inOutFile >> temp;
inOutFile.close();
}
return 0;
}
边栏推荐
- DCM 中间件家族迎来新成员
- 网络请求技术--跨域
- 傅立叶变换相关公式
- Navicat数据显示不完全的解决方法
- Reflex WMS中阶系列7:已经完成拣货尚未Load的HD如果要取消拣货,该如何处理?
- Moonbeam与Project Galaxy集成,为社区带来全新的用户体验
- from origin ‘null‘ has been blocked by CORS policy Cross origin requests are only supported for
- Interview: Briefly describe a project you are involved in
- Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
- 交返是做日内交易的必要条件
猜你喜欢
随机推荐
华为5年女测试工程师离职:多么痛的领悟...
傅立叶变换相关公式
当关注「互联网+」模式的时候,通常仅仅只是在关注「互联网+」模式本身
flex布局中使用flex-wrap实现换行
H5画布 canvas(一)canvas简介、绘制圆形矩形、案例饼状图绘制
关于MySQL的数据插入(高级用法)
datagrip连接mysql数据库
Detailed explanation of fastjson
Use flex-wrap to wrap lines in flex layout
fastjson详解
JS中对事件代理的理解及其应用场景
GO GOPROXY代理设置
ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法!
【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
一本适合职场新人的好书
网络请求技术--跨域
管理基础知识11
Flask gets post request parameters
期货公司开户实力经纪业务的规模