C++IO设备读写
创始人
2024-03-31 06:49:58
0

1 输入输出IO流

1.1 图解输入输出流

IO设备:文件、终端(dos黑框框)、特殊的数据类型(streamstring)
在这里插入图片描述

1.2 输入输出流类库

C++中的输入输出流是靠定义好的类库来操作的

在这里插入图片描述

2 文件读写操作

2.1 文件的打开方式

在这里插入图片描述

2.2 头文件

头文件:fstream
ofstream:读写
istream:读操作
of

2.2 文本文件读写

使用ofstream来写文本

#include
#include
#includeusing namespace std;int main() {ofstream outfile;string name;int age;cin >> name >> age;outfile.open("C:/Users/98207/desktop/test.txt", ios::out);  // 写入文件,没有文件会新疆,默认文件是截断的outfile << name << endl;outfile << age;outfile.close();return 0;
}

在这里插入图片描述

使用ifstream读取文件

程序:

#include
#include
#include
#includeusing namespace std;int main() {ifstream infile;string str;int age;infile.open("C:/Users/98207/desktop/test.txt", ios::in);  // 读取文件while (1) {if (infile.eof()) {break;}infile >> str;cout << str << endl;;// getline(infile, str);// cout << str << endl;}infile .close();return 0;
}

结果:

bian
12

使用fstream来读写文件

写入文件

#include
#include
#include
#includeusing namespace std;int main() {string name;int age;fstream outfile;outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out);cin >> name >> age;outfile << name << endl;outfile << age;outfile.close();return 0;
}

在这里插入图片描述

读取文件

#include
#include
#include
#includeusing namespace std;int main() {string str;fstream infile;infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);while (1) {if (infile.eof()) {break;}infile >> str;cout << str << endl;}infile.close();return 0;
}

在这里插入图片描述

2.3 二进制的读写

二进制和文本写区别在于数字二进制数字把实际字节数写入进去。
比如9,那么写入的是4个char字符0009至于大小端看电脑。

2.3.1 二进制写

#include
#include
#includeusing namespace std;int main() {fstream outfile;char name[20];int age;// 为什么保存格式是dat,因为使用文本格式会按照文本格式解析outfile.open("C:/Users/98207/Desktop/1.dat", ios::trunc | ios::out | ios::binary);cin >> name >> age;outfile << name << '\t';outfile.write((char*)&age, sizeof(age));  // 二进制写outfile.close();return 0;
}

在这里插入图片描述

2.3.2 二进制读

#include
#include
#includeusing namespace std;int main() {fstream infile;char name[20];char temp;int age;infile.open("C:/Users/98207/Desktop/1.dat", ios::in | ios::binary);infile >> name;infile.read((char*)&temp, sizeof(temp));  // 丢弃制表符infile.read((char*)&age, sizeof(age));cout << name << '\t' << age << endl;infile.close();return 0;
}

在这里插入图片描述

2.4 按照特殊格式读写

2.4.1 特殊格式写入

#include
#include  //ofstream
#include  // stringstreamusing namespace std;int main() {stringstream ret;ofstream outfile;string name;int age;outfile.open("C:/Users/98207/Desktop/test2.txt", ios::out | ios::trunc);while (1) {cin >> name >> age;if (cin.eof()) {break;}ret << name << "\t\t\t" << age << endl;  // ret会累积// outfile << ret.str();// ret.clear();}outfile << ret.str();outfile.close();return 0;}

在这里插入图片描述

2.4.2 特殊格式读取

#include
#include
#include  // getline, stringusing namespace std;int main() {fstream infile;string str;char name[20];int age;infile.open("C:/Users/98207/Desktop/test2.txt", ios::in);while (1) {getline(infile, str);if (infile.eof()) {break;}sscanf_s(str.c_str(), "%s %d", name, sizeof(name), & age);  // 这里的参数只能是char类型,这里的空格会替换文件的制表符或者空格cout << name << "\t\t\t" << age << endl;}infile.close();return 0;
}

在这里插入图片描述

2.5 文件流标志

这里常用的就是is_open()和eof()
在这里插入图片描述

2.6 文件指针

seekg

tellg

seekp

未完成

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...