当前位置: 首页 > article >正文

【32】C++流

目录

C++流

读写文件:文件流

对文本文件流读写 

写文本文件

读文本文件

对二进制文件流读写

写二进制文件

 读二进制文件

 对文件流按格式读写取数据

按指定格式写文件

按指定格式读文件


C++流

IO: 向设备输入数据和输出数据

C++的IO流

设备: 

  1. 文件
  2. 控制台
  3. 特定的数据类型(stringstream)

c++中,必须通过特定的已经定义好的类, 来处理IO(输入输出)

读写文件:文件流

文件流: 对文件进行读写操作

头文件:  <fstream>

类库:

   ifstream    对文件输入(读文件)

   ofstream    对文件输出(写文件)

   fstream     对文件输入或输出

对文本文件流读写 

文件打开方式:

以上打开方式, 可以使用位操作 |  组合起来

写文本文件

(自编代码)

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
	ofstream writefile;
	writefile.open("flie.txt");
	string name;
	int age;

	while (1) {
		cout << "please input your name:[Ctrl+z exit]";
		cin >> name;
		if (cin.eof()) {
			break;
		}
		writefile << name << "\t";
		cout << "please input your age:";
		cin >> age;
		writefile << age << endl;
	}
	
	writefile.close();

	system("pause");
	return 0;
}

读文本文件

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
	ifstream outfile;
	outfile.open("flie.txt");
	string name;
	int age;

	while (1) {
		outfile >> name;
		if (outfile.eof()) {
			break;
		}
		cout << name << "\t";
		
		outfile >> age;
		cout << age << endl;
	}

	outfile.close();

	system("pause");
	return 0;
}

对二进制文件流读写

思考:

文本文件和二进制文件的区别?

文本文件: 写数字1,  实际写入的是 ‘1’

二进制文件:写数字1, 实际写入的是  整数1(4个字节,最低字节是1, 高3个字节都是0)

                     写字符‘R’实际输入的还是‘R’

写二进制文件

使用文件流对象的write方法写入二进制数据.

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
	ofstream bitWriteFile;
	bitWriteFile.open("flie.bat");
	string name;
	int age;

	
	while (1) {
		cout << "please input your name [Ctrl+z exit] :";
		cin >> name;
		
		if (cin.eof()) {
			break;
		}
		bitWriteFile << name << "\t";

		cout << "please input your age :";
		cin >> age;
		bitWriteFile.write((char*)&age, sizeof(age));
		
	}

	bitWriteFile.close();

	system("pause");
	return 0;
}
bitWriteFile.write((char*)&age, sizeof(age));

这行代码是在C++程序中将一个整数 `age` 以二进制形式写入到文件中的操作。让我们详细解释一下这行代码的各个部分:

1. `bitWriteFile`:这是一个 `ofstream` 类型的对象,它是C++标准库中的一个类,用于提供对文件的输出功能。

2. `.write`:这是 `ofstream` 对象的一个成员函数,用于写入数据。`.write` 函数接受两个参数:第一个参数是一个指向要写入数据的指针,第二个参数是要写入的数据的大小(以字符为单位)。

3. `(char*)&age`:这是一个类型转换操作,它将整数 `age` 的地址转换为一个 `char*` 类型的指针。这样做的原因是 `.write` 函数需要一个指向字符数据的指针。由于C++中的类型安全性,直接传递一个整数的地址是不合法的,因此需要将其转换为 `char*` 类型。`&age` 获取 `age` 变量的内存地址,然后通过强制类型转换 `(char*)`,告诉编译器将这个地址当作字符数组的首地址来处理。

4. `sizeof(age)`:这是一个编译时运算符,它返回变量 `age` 所占的字节数。对于一个 `int` 类型的变量,`sizeof(age)` 通常返回 4(在32位系统中)或 8(在64位系统中),这取决于编译器和操作系统的架构。这个值告诉 `.write` 函数需要写入多少字节的数据。

综合起来,`bitWriteFile.write((char*)&age, sizeof(age));` 这行代码的作用是以二进制形式将 `age` 变量的内容写入到 `bitWriteFile` 指定的文件中。这种方式写入的数据不是以文本形式存储的,而是直接存储 `age` 变量在内存中的二进制表示。这意味着,如果你在不同的机器或不同的编译器上读取这个文件,可能会遇到字节序(endianness)或整数大小不一致的问题,导致读取的值不正确。因此,使用二进制文件进行数据交换时,需要确保写入和读取数据的系统环境是兼容的。

 读二进制文件

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

int main(void) {
	ifstream bitOutFile;
	bitOutFile.open("flie.bat", ios::in | ios::binary);
	string name;
	int age;


	while (1) {
		bitOutFile >> name;
		if (bitOutFile.eof()) {
			break;
		}
		cout << name << "\t";
		
		char temp;
		bitOutFile.read((char*)&temp,sizeof(temp));
		
		bitOutFile.read((char*)&age, sizeof(age));
		cout << age << endl;

	}

	bitOutFile.close();

	system("pause");
	return 0;
}

 对文件流按格式读写取数据

使用stringstream

按指定格式写文件

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
	string name;
	int age;
	ofstream outfile;
	outfile.open("file.txt", ios::out | ios::trunc);

	while (1) {
		cout << "请输入姓名: [ctrl+z退出] ";
		cin >> name;
		if (cin.eof()) { //判断文件是否结束
			break;
		}

		cout << "请输入年龄: ";
		cin >> age;

		stringstream s;
		s << "name:" << name << "\t\tage:" << age << endl;
		outfile << s.str();
	}

	// 关闭打开的文件
	outfile.close();

	system("pause");
	return 0;
}

按指定格式读文件

#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <Windows.h>

using namespace std;

int main(void)
{
	char name[32];
	int age;
	string line;
	ifstream infile;
	infile.open("file1.txt");

	while (1) {
		getline(infile, line);
		if (infile.eof()) { //判断文件是否结束
			break;
		}

		sscanf_s(line.c_str(), "name:%s\t\tage:%d", name, sizeof(name), &age);
		cout << "姓名:" << name << "\t\t年龄:" << age << endl;
	}

	infile.close();

	system("pause");
	return 0;
}


http://www.kler.cn/a/369315.html

相关文章:

  • SAP 条件记录简介
  • 关于嵌入式学习的一些短浅经验
  • C++:字符串
  • 【java】java的基本程序设计结构04-数值类型的转换
  • Spring Boot:植物健康监测的智能先锋
  • 什么是AI神经网络?
  • ETLCloud+Doris组合:数据集成,更简单更高效
  • Linux系统基础-进程间通信(5)_模拟实现命名管道和共享内存
  • 【ubuntu18.04】ubuntu18.04 编译LightGBM操作说明
  • 大众点评 web mtgsig 1.2分析
  • AI跟踪报道第62期-本周AI新闻: 微软推出Copilot的AI Agent和Computer Control
  • 【学术会议投稿】Imagen:重塑图像生成领域的革命性突破
  • 深入理解Rust中的指针:裸指针 智能指针
  • Docker:容器
  • 2024 AI 时代:科学计算服务器——科技创新核心动力源
  • k8s 二进制部署安装(三)
  • 08 实战:色彩空间展示(本程序以视频为主)
  • 基于 matlab 计算 TPI(地形位置指数)
  • 2024-10-24 问AI: [AI面试题] 解释自然语言处理 (NLP) 的概念
  • AAPL: Adding Attributes to Prompt Learning for Vision-Language Models
  • RTOS性能测试:R-Rhealstone
  • 从可逆计算看低代码
  • C语言复习总结超详细版(1)小白转身即变 有实例超级详细
  • 多态的体现
  • 【数据结构与算法】第5课—数据结构之双链表
  • 网络原理之 TCP解释超详细!!!