練習寫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<




關於其他壓縮格式的文件以後在繼續學習。


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