当前位置:网站首页>ofstream,ifstream,fstream read and write files
ofstream,ifstream,fstream read and write files
2022-08-02 02:09:00 【wonderful】
系列文章目录
前言
Reading and writing files is often used in programming,基本都是使用ofstream,ifstream,fstream
一、ofstream
#include <fstream>
ofstream //文件写操作 内存写入存储设备
ifstream //文件读操作,存储设备读区到内存中
fstream //读写操作,对打开的文件可进行读写操作

在ofstream类中,成员函数open()实现打开文件的操作,从而将数据流和文件进行关联.The following code opens in turn0~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;
}

Output content of each file
注意:If the code is written as follows:
也就是说:ofstream初始化的时候,don't specify file,在调用openfunction and specify the file name,Otherwise the contents of the file are empty,切记切记.It took me a morning to find out the problem,血淋淋的教训啊!!!!
Also, remember to close the file when you are done writing
二、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 Classes for all built-in data types as well std::string 和 std::complex 类型重载 << 和 >> 操作符.The following examples demonstrate the use of these operators:
#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;
}
边栏推荐
猜你喜欢

Project Background Technology Express

3. Bean scope and life cycle

密码学的基础:X.690和对应的BER CER DER编码
![[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)](/img/ed/ffced88c9d23c20ccf380494051381.jpg)
[ORB_SLAM2] void Frame::ComputeImageBounds(const cv::Mat & imLeft)

Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas

Win Go development kit installation configuration, GoLand configuration

Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program

typescript34-class的基本使用

MySQL optimization strategy

The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
随机推荐
AOF rewrite
Record the pits where an error occurs when an array is converted to a collection, and try to use an array of packaging types for conversion
Chengdu openGauss user group recruit!
Redis 订阅与 Redis Stream
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
检查IP或端口是否被封
【LeetCode每日一题】——704.二分查找
秒懂大模型 | 3步搞定AI写摘要
成都openGauss用户组招募啦!
typescript30-any类型
Software testing Interface automation testing Pytest framework encapsulates requests library Encapsulates unified request and multiple base path processing Interface association encapsulation Test cas
Use baidu EasyDL implement factory workers smoking behavior recognition
Redis 持久化 - RDB 与 AOF
【Brush the title】Family robbery
Constructor instance method inheritance of typescript38-class (implement)
Golang分布式应用之Redis
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
nacos启动报错,已配置数据库,单机启动
Constructor of typescript35-class
The underlying data structure of Redis
