IO操作(Num22)

目录

一、流的概念:

[二、 fstream的使用:](#二、 fstream的使用:)

打开方式:

代码示例1:

代码示例2:

三、文件打包:

代码示例:


一、流的概念:

流是一种抽象的概念,表示了数据的无结构化传递,由一端流向另一端常见的:

  1. ++输入/输出流(I/O流)++:键盘向程序内部输入,程序内部向屏幕输出

  2. ++数据流++:通常指网络中的数据传递

  3. ++文件流++:通常指读写文件

二、 fstream的使用:

fstream是C++中常用的文件操作类,用于文件操作,和C语言学过的文件操作作用一样

使用:

  1. 包含头文件 fstream,并打开命名空间std 或是std::fstream;

  2. 使用fstream类来实例化对象,通过对象的成员来进行文件操作

打开方式:

open(文件路径,打开方式)

  1. 文件路径:绝对路径或相对路径

  2. 打开方式:表示打开文件的模式,不同的模式对应不同的数据操作(只读,只写,二进制操作等)

  • ios::in 以只读方式打开文件(默认方式)

  • ios::out 以写入方式打开文件,如果文件不存在则创建新文件

  • ios::app 以追加方式打开文件,在文件末尾追加写入内容

  • ios::ate 打开文件后定位到文件末尾,可以随机访问并修改文件内容

  • ios::trunc 如果文件存在,则先删除文件内容,再以写入方式打开文件

  • ios::binary 以二进制方式打开文件,用于处理二进制文件

  • ios::sin | ios::put 以读写方式打开文件,在文件的任意位置进行读写操作

代码示例1:

cpp 复制代码
#include<iostream>
#include<fstream>

using namespace std;

int main() {
	fstream file;
	file.open("1.txt",ios::in|ios::out);
	//cout<<file.is_open();//判断文件是否打开了
	//file.put('c');
	if (!file.is_open()) {
		cout << "文件打开失败" << endl;
	}
	file.seekp(0,ios::end);//文件最末尾
	int size = file.tellp();

	file.seekp(0, ios::beg);//文件最末尾
	int z = 0;
	while (!file.eof()) {//判断文件指针是否到了末尾
		char c[100] = { 0 };
		file.getline(c, 99);
		z += 2;
		cout << "读取进度:"<<size<<"/"<<z<<endl;
	}
	cout << "读取进度:" << size << "/" << size << endl;

	cout<<file.tellp();

	char c[100] = {0};
	//file.get(c,99);//只能读一行,\n不会读换行
	//file.get();
	//file.get(c, 99,'r');
	//cout << c;
	file.getline(c, 99);//读取一行换行也读进来
	cout << c;
	file.getline(c, 99);
	cout << c;
	file.close();
	return 0;
}

代码示例2:

cpp 复制代码
#include<iostream>
#include<fstream>

using namespace std;

int main() {
	fstream file;
	file.open("1.txt", ios::in | ios::out);
	if (!file.is_open()) {
		cout << "文件打开失败"<<endl;
	}

	int wage = 18;
	char wname[] = "XXX";

	file << wage << "\t" << wname << endl;
	file << 20 << "\t" << "XXXXX" << endl;

	file.seekp(0, ios::beg);

	int age = 0;
	char str[10] = { 0 };

	file >> age >> str;
	cout << age << "   " << str<<endl;
	file >> age >> str;
	cout << age << "   " << str<<endl;
	file >> age >> str;
	cout << age << "   " << str<<endl;

	//int wid = 100;
	//file.write((const char*)&wage, sizeof(int));

	//int namesize = strlen(wname);

	//file.write((const char*)&namesize, sizeof(int));
	//file.write(wname, strlen(wname));

	//file.write((const char*)&wid, sizeof(int));

	//file.seekp(0, ios::beg);

	//int rage = 0;
	//char rname[10] = { 0 };
	//int rid = 0;
	//int x;
	//file.read((char*)&rage, sizeof(int));
	//file.read((char*)&x, sizeof(int));
	//file.read(rname, 4);//这里不能写数组的长度应该为读取内容的长度
	//file.read((char*)&rid, sizeof(int));

	//cout << rage << " " << rname << " " << rid << endl;


	/*char str[] = "这是一行字符串";
	file.write(str,strlen(str));*/

	//int x = 10;

	//file.write((const char*)&x,sizeof(int));

	//file.seekp(0, ios::beg);

	//int y = 0;
	//file.read((char*)&y, sizeof(int));

	//cout << y << endl;

	file.close();
	return 0;
}

三、文件打包:

把多个文件以某种格式写入到文件中,让别人不能随意获取使用

代码示例:

cpp 复制代码
#include<iostream>
#include<fstream>
using namespace std;

struct node
{
	int filesize;
	int namesize;
	char name[20];
};


void dabao() {
	node n[6] = {
		{0,5,"1.jpg"},
		{0,5,"2.jpg"},
		{0,5,"3.jpg"},
		{0,5,"4.jpg"},
		{0,5,"5.jpg"},
		{0,5,"6.jpg"}
	};

	fstream file[6];
	for (int i = 0; i < 6; i++) {
		file[i].open(n[i].name, ios::in);
		file[i].seekg(0, ios::end);
		n[i].filesize = file[i].tellg();
		file[i].seekg(0,ios::beg);
	}

	fstream outfile("1.pack", ios::out);
	int size = 6;

	for (int i = 0; i < 6; i++) {
		outfile.write((char*)&n[i].filesize, sizeof(int));
		outfile.write((char*)&n[i].namesize, sizeof(int));
		outfile.write(n[i].name, n[i].namesize);
	}

	for (int i = 0; i < 6; i++) {
		while (!file[i].eof())
		{
			outfile.put(file[i].get());
		}
		file[i].close();
	}
	outfile.close();
}

void chaibao() {
	fstream file("1.pack", ios::binary);
	int size = 0;
	file.read((char*)&size,sizeof(int));

	node* n = new node[size];
	memset(n, 0, sizeof(node) * size);

	fstream* outfile = new fstream[size];

	for (int i = 0; i < size; i++) {
		file.read((char*)&n[i].filesize, sizeof(int));
		file.read((char*)&n[i].namesize, sizeof(int));
		file.read(n[i].name, n[i].namesize);

		outfile[i].open(n[i].name, ios::out | ios::binary);
		
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j <= n[i].filesize; j++) {
				outfile[i].put(file.get());
			}
			outfile[i].close();
		}
		file.close();
		delete[] n;
		delete[] outfile;
	}
}

int main() {
	dabao();
	chaibao();
	return 0;
}
相关推荐
Echo缘4 小时前
嵌入式系统C语言资源分类与内存分布分析
c语言·开发语言
胖大和尚4 小时前
C++ 多线程编程的实现方式
c++·thread
JaneConan4 小时前
鸿蒙 ArkUI 深水区:@Watch 和 @Observed,状态变了「自动跑」+ 嵌套对象「深层重绘」
开发语言·后端·ui·harmonyos
赤水无泪4 小时前
qt中图标、名称的设置方式
开发语言·qt
王莎莎-MinerU5 小时前
MCP 解决的是工具接入,科研 Agent 还缺的是科学证据接口标准化
开发语言·网络·人工智能·深度学习·pdf·c#·php
橘子海全栈攻城狮5 小时前
【最新源码】基于SpringBoot + Vue的超市管理系统的设计与实现D002
java·开发语言·vue.js·spring boot·后端·spring
在水一缸5 小时前
深入浅出 Catch2:现代 C++ 测试框架的优雅实践
开发语言·c++·单元测试·log4j·测试框架·catch2
-银雾鸢尾-5 小时前
C#中Object类内的方法
开发语言·c#
2401_841495645 小时前
【数据结构】B*树
数据结构·c++·b树·算法·删除·插入·三分分裂
浮江雾5 小时前
Flutter第十节-----Flutter布局与组件全解析
android·开发语言·前端·学习·flutter·入门