c++文件流基本用法(fstream, ifstream, ostream)

前言:
c++的文件流处理其实很简单,前提是你能够理解它。文件流本质是利用了一个buffer中间层。有点类似标准输出和标准输入一样。

c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。

有错误和疏漏的地方,欢迎批评指证。

需要包含的头文件: <fstream> 

名字空间: std

也可以试用<fstream.h>

fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。
ifstream -- 从已有的文件读

ofstream -- 向文件写内容

fstream - 打开文件供读写

支持的文件类型

实际上,文件类型可以分为两种: 文本文件和二进制文件.

文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。

 

例一: 写文件

声明一个ostream变量

  1. 调用open方法,使其与一个文件关联
  2. 写文件
  3. 调用close方法.
    
    
  1. #include <fstream.h>
  2.  
  3. void main
  4. {
  5. ofstream file;
  6.  
  7. file. open ( "file.txt" );
  8.  
  9. file<< "Hello file/n"<< 75;
  10.  
  11. file. close ( );
  12. }

可以像试用cout一样试用操作符<<向文件写内容.
Usages:

    
    
  1.  
  2. file<< "string/n";
  3. file. put ( 'c' );

例二:  读文件

1. 声明一个ifstream变量.

2. 打开文件.

3. 从文件读数据

4. 关闭文件.

    
    
  1. #include <fstream.h>
  2.  
  3. void main
  4. {
  5. ifstream file;
  6. char output [ 100 ];
  7. int x;
  8.  
  9. file. open ( "file.txt" );
  10.  
  11. file>>output;
  12. cout<<output;
  13. file>>x;
  14. cout<<x;
  15.  
  16. file. close ( );
  17. }
    同样的,你也可以像cin一样使用>>来操作文件。或者是调用成员函数
    Usages:
    
    
  1.  
  2. file>>char *;
  3. file>>char;
  4. file. get ( char );
  5. file. get ( char *, int );
  6. file. getline ( char *, int sz );
  7. file. getline ( char *, int sz, char eol );

1.同样的,你也可以使用构造函数开打开一个文件、你只要把文件名作为构造函数的

第一个参数就可以了。

    
    
  1. ofstream file ( "fl.txt" );
  2. ifstream file ( "fl.txt" );

上面所讲的ofstream和ifstream只能进行读或是写,而fstream则同时提供读写的功能。
void main()

    
    
  1. {
  2. fstream file;
  3.  
  4. file. open ( "file.ext",iso:: in|ios:: out )
  5.  
  6. //do an input or output here
  7.  
  8. file. close ( );
  9. }

open函数的参数定义了文件的打开模式。总共有如下模式

    
    
  1. 属性列表
  2.  
  3. ios:: in
  4. ios:: out
  5. ios:: app 从文件末尾开始写
  6. ios:: binary 二进制模式
  7. ios:: nocreate 打开一个文件时,如果文件不存在,不创建文件。
  8. ios:: noreplace 打开一个文件时,如果文件不存在,创建该文件
  9. ios:: trunc 打开一个文件,然后清空内容
  10. ios:: ate 打开一个文件时,将位置移动到文件尾


Notes

  • 默认模式是文本
  • 默认如果文件不存在,那么创建一个新的
  • 多种模式可以混合,用|(按位或)
  • 文件的byte索引从0开始。(就像数组一样)

我们也可以调用read函数和write函数来读写文件。

 

文件指针位置在c++中的用法:

    
    
  1. ios:: beg 文件头
  2. ios:: end 文件尾
  3. ios:: cur 当前位置
    例子:
    
    
  1. file. seekg ( 0,ios:: end );
  2.  
  3. int fl_sz = file. tellg ( );
  4. file. seekg ( 0,ios:: beg );

常用的错误判断方法:

    
    
  1. good ( ) 如果文件打开成功
  2. bad ( ) 打开文件时发生错误
  3. eof ( ) 到达文件尾
    例子:
    
    
  1. char ch;
  2. ifstream file ( "kool.cpp",ios:: in|ios:: out );
  3.  
  4. if ( file. good ( ) ) cout<< "The file has been opened without problems;
  5. else cout<<"An Error has happend on opening the file;
  6.  
  7. while (! file. eof ( ) )
  8. {
  9. file>>ch;
  10. cout<<ch;
  11. }

 

  • 34
    点赞
  • 180
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值