C++ IO设备读写功能实现详解

 更新时间:2022年11月07日 09:06:00   作者:划水猫  
C++的文件IO(Input,Output)操作就是指对文件进行读写(输入与输出)的操作。输入就是从磁盘上的文件中读取内容到内存中。输出就是将内存中的数据内容输出或者说写入到磁盘的文件中,这篇文章主要介绍了C++ IO设备读写功能实现

1 输入输出IO流

1.1 图解输入输出流

IO设备:文件、终端(dos黑框框)、特殊的数据类型(streamstring)

1.2 输入输出流类库

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

2 文件读写操作

2.1 文件的打开方式

2.2 文件读写类库的头文件

头文件:fstream

ofstream:读写

istream:读操作

ofstream:写操作

2.3 文本文件读写

使用ofstream来写文本

ofstream写入文件默认打开方式是ios::trunc,即没有文件那么创建,该文件存在并且有内容会直接清空内容

#include<iostream>
#include<windows.h>
#include<fstream>
using 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<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using 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来读写文件

写入文件fstream默认不会截断文件

#include<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using 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<iostream>
#include<windows.h>
#include<string>
#include<fstream>
using 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.4 二进制的读写

二进制和文本写区别在于数字,二进制数字是把实际字节数写入进去。

比如整数9,那么写入的是4个char字符0009,至于存储的大小端方式要看cpu。

2.4.1 二进制写

#include<iostream>
#include<string>
#include<fstream>
using 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.4.2 二进制读

#include<iostream>
#include<string>
#include<fstream>
using 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.5 按照特殊格式读写

2.5.1 特殊格式写入

#include<iostream>
#include<fstream>  //ofstream
#include<sstream>  // stringstream
using 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.5.2 特殊格式读取

#include<iostream>
#include<fstream>
#include<string>  // getline, string
using 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.6 文件流标志

这里常用的就是is_open()和eof()

2.7 文件指针

输入流指针seekg

原形:basic_istream& seekg( off_type off, // 偏移量

std::ios_base::seekdir dir); // 起始位置

作用:设置输入流位置

参数1:偏移量

参数2:相对位置

  • beg 相对于开始位置
  • cur 相对于当前位置
  • end 相对于结束位置

从开始位置文件指针偏移5个字节,然后读取内容

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
	// ofstream infile;
	ifstream infile;
	string str;
	infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
	infile.seekg(5, ios::beg);  // 从开始位置偏移5个字节
	while (1) {
		getline(infile, str);
		cout << str;
		if (infile.eof()) {
			break;
		}
	}
	infile.close();
	return 0;
}

输入流指针tellg

作用:返回输入流的当前位置(距离文件的起始位置的偏移量)

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
	ifstream infile;
	string str;
	infile.open("C:/Users/98207/Desktop/1.txt", ios::in);
	infile.seekg(5, ios::beg);  // 设置偏移量位5个字节
	cout << "文件指针偏移量:" << infile.tellg() << endl;  // 相对于文件起始位置
	infile.close();
	return 0;
}

结果:

文件指针偏移量:5

E:\Microsoft Visual Studio\code\Project15\x64\Debug\Project15.exe (进程 68440)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

输出流指针seekp

作用:设置输出流位置

函数原形:basic_ostream& seekp( off_type off, // 偏移量

std::ios_base::seekdir dir ); // 起始位置

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
	ofstream outfile;
	outfile.open("user1.txt", ios::out | ios::trunc);
	outfile << "123456789";
	outfile.seekp(3, ios::beg);  // 指针先指向开头,然后向后偏移三个字节
	outfile << "ABC";
	outfile.close();
	return 0;
}

到此这篇关于C++ IO设备读写功能实现详解的文章就介绍到这了,更多相关C++ IO设备读写内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • c++详细讲解构造函数的拷贝流程

    c++详细讲解构造函数的拷贝流程

    拷贝构造函数是一种特殊的构造函数,它在创建对象时,是使用同一类中之前创建的对象来初始化新创建的对象。拷贝构造函数通常用于:通过使用另一个同类型的对象来初始化新创建的对象。 复制对象把它作为参数传递给函数。复制对象,并从函数返回这个对象
    2022-05-05
  • C++使用opencv处理两张图片的帧差

    C++使用opencv处理两张图片的帧差

    这篇文章主要为大家详细介绍了C++使用opencv处理两张图片的帧差,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • 一文带你掌握C语言中的文件操作

    一文带你掌握C语言中的文件操作

    文件通常是驻留在外部介质(如磁盘等)上的,在使用时才调入内存中来,本文主要来和大家介绍一下C语言中的文件操作,有需要的可以了解下
    2024-02-02
  • C++结构体struct和类class区别详解

    C++结构体struct和类class区别详解

    struct和class有什么区别?最本质的一个区别就是默认的访问控制:默认的继承访问权限,struct是public的,class是private的。
    2017-11-11
  • C语言猜凶手及类似题目的实现示例

    C语言猜凶手及类似题目的实现示例

    本文主要介绍了C语言猜凶手及类似题目的实现示例,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • C语言学生成绩管理系统课程设计

    C语言学生成绩管理系统课程设计

    这篇文章主要为大家详细介绍了C语言学生成绩管理系统课程设计,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C语言数据结构之中缀树转后缀树的实例

    C语言数据结构之中缀树转后缀树的实例

    这篇文章主要介绍了C语言数据结构之中缀树转后缀树的实例的相关资料,需要的朋友可以参考下
    2017-08-08
  • C语言顺序表的基本结构与实现思路详解

    C语言顺序表的基本结构与实现思路详解

    顺序表是用一段物理地址连续的存储单元依次存储数据元素的线性结构,一般情况下采用数组存储。本文将通过示例为大家讲解一下顺序表的基本操作,需要的可以参考一下
    2023-02-02
  • C语言数据结构与算法时间空间复杂度基础实践

    C语言数据结构与算法时间空间复杂度基础实践

    这篇文章主要为大家介绍了C语言数据结构与算法中时间空间复杂度的基础实践,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • C/C++题解LeetCode1295统计位数为偶数的数字

    C/C++题解LeetCode1295统计位数为偶数的数字

    这篇文章主要为大家介绍了C/C++题解LeetCode1295统计位数为偶数的数字示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01

最新评论