当前位置:网站首页>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;
}
边栏推荐
- Use baidu EasyDL implement factory workers smoking behavior recognition
- 数据链路层的数据传输
- 记录一次数组转集合出现错误的坑点,尽量使用包装类型数组进行转换
- 项目后台技术Express
- 雇用WordPress开发人员:4个实用的方法
- Named parameter implementation of JDBC PreparedStatement
- leetcode/字符串中的变位词-s1字符串的某个排列是s2的子串
- libcurl访问url保存为文件的简单示例
- After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
- LeetCode Review Diary: 34. Find the first and last position of an element in a sorted array
猜你喜欢
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree

Typescript31 - any type

十字光标太小怎么调节、CAD梦想画图算量技巧

Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...

Handwritten Blog Platform ~ Day Two

Speed up your programs with bitwise operations

Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem

Constructor of typescript35-class

LeetCode刷题日记:LCP 03.机器人大冒险

MySQL8 下载、启动、配置、验证
随机推荐
LeetCode Brushing Diary: 74. Searching 2D Matrix
十字光标太小怎么调节、CAD梦想画图算量技巧
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
编码经验之谈
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
PHP 使用 PHPRedis 与 Predis
C语言之插入字符简单练习
Fly propeller power space future PIE - Engine Engine build earth science
Rasa 3.x 学习系列- Rasa - Issues 4873 dispatcher.utter_message 学习笔记
Handwritten Blog Platform ~ Day Two
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
Chengdu openGauss user group recruit!
libcurl访问url保存为文件的简单示例
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
【 wheeled odometer 】
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
Typescript31 - any type
¶ Backtop back to the top is not effective
nacos启动报错,已配置数据库,单机启动
MySQL8 download, start, configure, verify
