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;
}
相关推荐
一点七加一3 小时前
Harmony鸿蒙开发0基础入门到精通Day07--JavaScript篇
开发语言·javascript·ecmascript
人邮异步社区3 小时前
推荐几本学习计算机语言的书
java·c语言·c++·python·学习·golang
qq_574656254 小时前
java后端初始化模版
java·开发语言
毕设源码-朱学姐5 小时前
【开题答辩全过程】以 基于JAVA的市级非物质文化遗产交流平台为例,包含答辩的问题和答案
java·开发语言
ha20428941946 小时前
Linux操作系统学习之---线程池
linux·c++·学习
pedestrian_h6 小时前
操作系统-线程
android·java·开发语言
A-code6 小时前
C/C++ 中 void* 深度解析:从概念到实战
c语言·开发语言·c++·经验分享·嵌入式
国服第二切图仔8 小时前
Rust中泛型函数实现不同类型数据的比较
开发语言·后端·rust
技术砖家--Felix8 小时前
Spring Boot入门篇:快速搭建你的第一个Spring Boot应用
java·开发语言·音视频
国服第二切图仔8 小时前
Rust开发之使用Trait对象实现多态
开发语言·算法·rust