您的位置:首页 > 编程语言 > C语言/C++

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

2014-05-03 22:22 537 查看
首先是简单文件,使用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<




关于其他压缩格式的文件以后在继续学习。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 文件读取 压缩