加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

使用标准C++库std::fstream

(2011-08-17 13:51:46)
标签:

it

分类: 编程

标准C++操作文件主要是通过文件流fstream来完成的。文件输入/输出流用以实现文件读取和存储的操作,这种基于流的文件操作如图6-2所示。

http://hiphotos.baidu.com/shasha1918/pic/item/f2d3572cafcdddd08a13992b.jpg
(点击查看大图)图6-2 文件输入/输出流

和文件有关系的输入/输出类主要在头文件<fstream>中被定义。在这个头文件中主要定义了3个类。由这3个类控制对文件的各种输 入/输出操作,它们分别是ifstream、ofstream、fstream。其中fstream类是由iostream类派生而来的,它们之间的继承 关系如图6-3所示。

如果需要使用fstream,则需要包含<fstream>头文件,


http://hiphotos.baidu.com/shasha1918/pic/item/8a136327a0c2833f918f9d2b.jpg
(点击查看大图)图6-3 fstream与iostream


Example:

    #include <fstream>

    float i = 37896.8979;

    std::ofstream debugFile;
    debugFile.open("debug.txt");
    debugFile<< "Pass " << 1 << std::endl;
    debugFile<< "Pass " << 2 <<std::endl;
    debugFile<< "Pass " << "I Love u" << std::endl;
    debugFile<< "Pass " << 12.335 << std::endl;

     debugFile<< "Pass " << i << std::endl;
    debugFile.close();

Open debug.txt:

     Pass 1
     Pass 2
     Pass I Love u
     Pass 12.335
     Pass 37896.9




0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有