C++ 标准IO库 iostream,fstream,sstream

1.标准IO类
C++标准IO类型在三个独立的头文件中定义:iosstream定义读写控制窗口的类型,fstream定义读写已命名文件的类型,sstream多定义的类型则用于读写存储在内存中的string对象。下图为继承关系图:
C++标准IO库 - 蓝色雪狐 - 平安,幸福美满!
 2.国际化字符支持
扩展了wchar_t类型,占2个字节,相应的类为:wiostream,wostream,wistream等等,相应的标准输入输出对象,wcin,wcout等。每个IO头文件都定义了char和wchar_t类型。
3.IO对象不可复制或赋值,也就无法为参数或返回值。
4.IO标准库的条件状态
C++标准IO库 - 蓝色雪狐 - 平安,幸福美满!
  5.输出缓冲区的刷新方法
强制刷新:endl,flush,ends(用的较少),unitbuf操作符,
自动刷新:程序结束,缓冲区已满
将输入输出绑在一起:如果一个流调用tie函数将其本身绑在传递给tie的ostream实参对象上,则该流上的任何IO操作都会刷新实参所关联的缓冲区。
6.文件的输入和输出
ifstream:由istream派生而来,提供读文件的功能
ofstream:由ostream派生而来,提供写文件的功能
fstream:有iostream派生而来,提供读写同一个文件的功能
7.字符串流
istringstream:由istream派生而来,提供读string的功能
ostringstream:由ostream派生而来,提供写string的功能
stringstream:由iostream派生而来,提供读写string的功能

8.缓冲区,控制台:

8.1.io对象不可复制和赋值的。所以只能引用。

8.2.它有一些错误的标志,如strm::eofbit.可以通过s.eof()获取到01.这个以后要是有用,再细看吧。

8.3.cout<<"hell,I'm countryhu!"<<endl;是在输出串后加\n后刷新流,cout<<"hell,I'm countryhu!"<<flush;不加\n刷新流;如需刷新整个缓存则将混存数据放在unitbuf和nounitbuf之间。

8.4.读入前都会刷新输出。但输入与输出可以动态绑定。

cin.tie($cout);将cin与cout绑定

ostream *old_tie = cin.tie();

cin.tie(0);取消cin当前绑定

9.文件流:

9.1.ifstream infile;

infile.open(path.c_str()); 参数需转换为c类型。

if(!infile) //如果没有打开成功文件

9.2.ofstream outfile

outfile.open(path.c_str(), ofstream::app); 第二个参数默认为out,清空文件准备写。app为在文件问追加。

9.3.最好在打开一文件前,in.close();in.clear(); 关闭可能张打开的文件,和清空错误标志;

10.字符串流:

10.1.string line,word;

while(getline(cin, line))

{

    istringstream string_stream(line);

    while(string_stream >> word)

        cout<<word<<endl;

}

11.流之间可以相互转换。

11.1.stringstream word_stream("countryhu!");

cout << word_stream.str()<<endl;

word_stream.str("hi,countryhu!");

11.2.流读取一个字符stream.get()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将数据写成压缩文件 `.gz`,可以使用 C++ 标准中的压缩 zlib 和 IO gzip。 以下是一个将数据压缩并写入 `.gz` 文件的示例代码: ```cpp #include <iostream> #include <fstream> #include <sstream> #include <string> #include <cstring> #include <zlib.h> #include <gzstream.h> int main() { std::string data = "Hello, world!"; // 要压缩的数据 std::string filename = "compressed.gz"; // 压缩后的文件名 // 创建输出文件流 ogzstream outfile(filename.c_str(), std::ios::binary); // 初始化 zlib 压缩流 z_stream stream; memset(&stream, 0, sizeof(stream)); deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 16+MAX_WBITS, 8, Z_DEFAULT_STRATEGY); // 压缩并写入数据 stream.next_in = (Bytef*)data.c_str(); stream.avail_in = data.size(); int flush = Z_FINISH; int ret; do { char out[1024]; stream.next_out = (Bytef*)out; stream.avail_out = sizeof(out); ret = deflate(&stream, flush); outfile.write(out, sizeof(out) - stream.avail_out); } while (stream.avail_out == 0); deflateEnd(&stream); // 关闭文件流 outfile.close(); std::cout << "Data compressed and written to file: " << filename << std::endl; return 0; } ``` 上述代码中,使用 IO 中的 `ogzstream` 类创建了一个输出文件流 `outfile`,文件名为 `compressed.gz`。之后,初始化了一个压缩流 `stream`,使用 `deflateInit2()` 函数,其中 `16+MAX_WBITS` 表示使用 gzip 格式进行压缩。 然后,将要压缩的数据设置为 `stream.next_in`,并将 `stream.avail_in` 设置为数据大小。之后,使用 `deflate()` 函数进行压缩,并将压缩后的数据写入输出文件流中。最后,使用 `deflateEnd()` 函数结束压缩流。 需要注意的是,压缩后的数据可能比原始数据更大,因此在写入文件时需要使用二进制模式打开输出文件流,并且需要在写入文件前确定文件的大小。同时,需要在程序中包含 IO gzip 的头文件 `gzstream.h`。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值