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;
}
相关推荐
宋恩淇要努力4 小时前
C++继承
开发语言·c++
沿着路走到底5 小时前
python 基础
开发语言·python
沐知全栈开发6 小时前
C# 委托(Delegate)
开发语言
江公望6 小时前
Qt qmlRegisterSingletonType()函数浅谈
c++·qt
任子菲阳6 小时前
学Java第三十四天-----抽象类和抽象方法
java·开发语言
csbysj20207 小时前
如何使用 XML Schema
开发语言
R6bandito_7 小时前
STM32中printf的重定向详解
开发语言·经验分享·stm32·单片机·嵌入式硬件·mcu
逆小舟7 小时前
【C/C++】指针
c语言·c++·笔记·学习
earthzhang20217 小时前
【1007】计算(a+b)×c的值
c语言·开发语言·数据结构·算法·青少年编程