练习写C++代码(101)--简单的文件以及 .gz文件的读写

这篇博客介绍了如何在C++中使用fstream处理简单文件,以及结合zlib库进行.gz压缩文件的读写操作。通过实例展示了基本步骤,并提及未来将探索更多压缩格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先是简单文件,使用fstream中的方法。


///read_file.cpp

#include
#include
#include
using namespace std;

int main()
{
	ifstream input_file;
	ofstream output_file;

	input_file.open("/home/lisp/c++/hello.cpp");
	string str;
	input_file>>str;
	cout<


.gz压缩文件,需要安装zlib库,使用库中的方法将压缩文件读入。


///read_zip_file.cpp

#include
#include
#include
using namespace std;

///buffer size
const int GZ_BUF_SIZE = 1024;

int main(int argc, char** argv)
{
	///read gz filename from command argv[1]
	gzFile file = gzopen(argv[1],"rb");

	unsigned char buf[GZ_BUF_SIZE];
	int len;
	string out;
	while (len = gzread(file, buf, GZ_BUF_SIZE))
	{
		out.append((const char*)buf, len);
	}
	///close the file
	gzclose(file);

	cout<




关于其他压缩格式的文件以后在继续学习。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值