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

首先是简单文件,使用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<




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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章