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

未完成

相关内容

热门资讯

保存时出现了1个错误,导致这篇... 当保存文章时出现错误时,可以通过以下步骤解决问题:查看错误信息:查看错误提示信息可以帮助我们了解具体...
汇川伺服电机位置控制模式参数配... 1. 基本控制参数设置 1)设置位置控制模式   2)绝对值位置线性模...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
表格中数据未显示 当表格中的数据未显示时,可能是由于以下几个原因导致的:HTML代码问题:检查表格的HTML代码是否正...
本地主机上的图像未显示 问题描述:在本地主机上显示图像时,图像未能正常显示。解决方法:以下是一些可能的解决方法,具体取决于问...
表格列调整大小出现问题 问题描述:表格列调整大小出现问题,无法正常调整列宽。解决方法:检查表格的布局方式是否正确。确保表格使...
不一致的条件格式 要解决不一致的条件格式问题,可以按照以下步骤进行:确定条件格式的规则:首先,需要明确条件格式的规则是...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...