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

未完成

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...