IO流,文件解包

I/O流

知道流的概念,会使用fstream来对文件进行操作

一、流的概念

1、什么是流

1.流是一种抽象的概念,表示了数据的无结构化传递

2.c++流是指信息从外部输入设备(如键盘)向计算机内部(如内存)输入和从内存向外部输出设备(显示器)输出的过程

3.c++定义有了I/O标准类库,用以完成流操作的功能

二、fstream的作用

1、fstream的使用

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

2、c++文件操作

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

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

3、常用的成员函数

1.open("文件名路径",打开方式)//打开文件

文件路径:绝对路径,相对路径

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

文件操作

2.close()

关闭文件

3.is_open()

判断文件是否成功打开,成功返回1,失败返回0

4.eof()

判断文件是否到达文件末尾,到了文件末尾返回true,失败返回false

//读取整个文档
char str[1024] = { 0 };
	int i = 0;
	fstream file("1.txt", ios::in);
	while (!file.eof())//判断文件是否到达末尾,到了返回true,否则返回false
	{
		file.get(str[i++]);
	}
	cout << str << endl;

5.put(字符)

往文件中写入一个字符

#include<fstream>
fstream file;
file.open("1.txt",ios::out);
file.put('w');
char c='s';
file.put(c);

6.get()

//从文件中读取字符,有三种常用的形式
char ch;
file.get(ch);//读取一个字符,赋值给ch
ch=file.get();//读取一个字符,赋值给ch
get(char* str,int num,char delim='
');//读取num个字符,赋值给str,或在这个期间读到'
'结束
getline(char* str,int num,char delim='
');//读取num个字符,赋值给str,或在这个期间读到了'
'结束

get()和getline()的区别

都可以读取多个字符,get会把文件内遇到终结符留在输入流,所以需要使用get将终结符扔掉。getline自动把输入流中的终结符取消掉了,下次可以继续直接读取。读一行字符时,一般使用getline

class myfstream :public fstream//可以自己添加功能
{
public:
};

int main()
{
	fstream file("1.txt", ios::in);//打开文件用于读取数据,如果文件不存在,则打开错误
	//读取一行字符
	char str[100] = { 0 };
	file >> str;//中间遇到空格就读取完成
	file.getline(str, 100, 'd');//读取100个字符到str中(读的只是一行),遇到d就停止,最后一个参也可以不写
	cout << str << endl;

	//读取多行字符
	char str[2][100] = { 0 };
	file.getline(str[0], 100);
	file.getline(str[1], 100);
	cout << str[0] << endl;
	cout << str[1] << endl;



	file.close();
	system("pause");
	return 0;
}

7.seekp()或seekg()

//1.txt里面存的是 abcde    

fstream file("1.txt", ios::in);
	file.seekp(3, ios::cur);
	cout << (char)file.get() << endl;//d

	file.close();

文件指针的移动

file.seekp(5,ios::beg);//从文件开头的位置往后偏移5个字符

在这里,数字5,表示的是文件指针往后移动5个字节的位置,如果是-5,那么就是往前移动5个字节的位置

第二个参数是文件指针从哪里移动位置,一共有三个:

ios::beg-->文件开头

ios::end-->文件末尾

ios::cur-->文件指针当前的位置

8.tellg()或tellp()

获取文件指针的移动大小,文件指针可以移动

int len = file.tellg();//获取文件指针移动了多少

   //2.png的大小是33016字节
	fstream file("2.png", ios::binary|ios::in);
	file.seekp(0, ios::end);
	int size = file.tellp();
	cout << size << endl;//33016
	file.close();

9.二进制形式读写文件

//写入一个数字
	int a = 10;
	fstream file("1.txt", ios::binary | ios::out);//以二进制方式打开文件,若不指定此模式,则以文本模式打开。再用于写入数据

	//读出一个数字
	file.write((const char*)(&a), sizeof(int));//写入文件
	int a;
	fstream file("1.txt", ios::binary | ios::in);
	file.read((char*)&a, sizeof(int));
	cout << a << endl;

	//一个数字,异或同一个数两次,会得到它本身  45^98^98=45

	//写入字符串
	fstream file("1.txt", ios::binary | ios::out);
	int b = 0;
	char str[100] = "adjddss";
	b = strlen(str);
	file.write((const char*)(&b), sizeof(int));
	file.write(str, b);

	int b = 0;
	char str[100] = { 0 };
	fstream file("1.txt", ios::binary | ios::in);
	file.read((char*)&b, sizeof(int));
	cout << b << endl;
	file.read(str, b);
	cout << str << endl;


	file.close();

write()写入文件

write(const char* str,int str_size);//写入一个整数
int a=10;
file.write((const char*)&a,sizeof(int));

read()读取文件

read(char* str,int str_size);//读取一个整数
int a;
file.read((char*)&a,sizeof(int));//读取4个字节的整数,赋值给a
写入文件
#include<iostream>
#include<fstream>
using namespace std;

/*
文件操作可以一边读一边写,但是不建议这样做(文件指针分不清)
读写分开进行
打开方式要和操作方式一样,以只读的方式打开,那么就只读,写是不行的
*/


int main()
{
	fstream file;
	file.open("1.txt", ios::out);//打开文件用于写入数据,如果文件不存在,则新建该文件,如果文件原来存在,则打开时清除原来的内容
	if (file.is_open())//打开成功返回1,失败返回0
		cout << "打开成功" << endl;
	else
		cout << "打开失败" << endl;
	//写入字符
	file.put('s');
	char c = 'b';
	file.put(c);
	//写入字符串
	file << "awnjankjnks教你带你";//重载了<<运算符的
	file.close();//关闭文件
	system("pause");
	return 0;
}

三、使用重载的<<>>

1、使用运算符来进行文件操作的优点类似于I/O流中的cin和cout,只不过cin和cout作用在内存,而fstream是作用在文件

fstream file;
int x=666;
file.open("1.txt",ios::out);
file<<"输入输出"<<endl;
file<<x<<endl;//写入数字和文字,一共两行
file.close();
file.open("1.txt",ios::in);
char str[1024];
int temp;
file>>str>>temp;//从文件中读取
cout<<str<<temp<<endl;

四、解包文件

文件 1.pack

#include<iostream>
#include<fstream>
using namespace std;
struct node
{
	char name[20];
	int namesize;
	int size;
};
int main()
{
	fstream file("1.pack", ios::binary | ios::in);
	int filesize = 0;//文件数量
	file.read((char*)&filesize, sizeof(int));
	node n[6] = { 0 };

	for (int i = 0; i < filesize; i++)
	{
		file.read((char*)&n[i].namesize, sizeof(int));
		file.read((char*)&n[i].name, n[i].namesize);
		file.read((char*)&n[i].size, sizeof(int));
	}
	for (int i = 0; i < filesize; i++)
	{
		printf("%d	%s	%d
", n[i].namesize, n[i].name, n[i].size);
	}
	fstream filepng[6];
	for (int i = 0; i < filesize; i++)
	{
		filepng[i].open(n[i].name, ios::binary|ios::out);
	}
	for (int i = 0; i < filesize; i++)
	{
		for (int j = 0; j < n[i].size; j++)
		{
			filepng[i].put(file.get());
		}
		filepng[i].close();
	}
	file.close();
	system("pause");
	return 0;
}
原文地址:https://www.cnblogs.com/Kissfly123/p/14464117.html