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则同时提供读写的功能。voidmain()

 
  
  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. }
sstream和strstream以及fstream
2009-10-16 15:39

 

在C++有两种字符串流,也称为数组I/O流,一种在sstream中定义,另一种在strstream中定义。它们实现的东西基本一样。strstream里包含class strstreambuf;class istrstream;class ostrstream;class strstream;它们是基于C类型字符串char*编写的sstream中包含class istringstream;class ostringstream;class stringbuf;class stringstream;class …….它们是基于std::string编写的
因此ostrstream::str()返回的是char*类型的字符串而ostringstream::str()返回的是std::string类型的字符串在使用的时候要注意到二者的区别,一般情况下推荐使用std::string类型的字符串当然如果为了保持和C的兼容,使用strstream也是不错的选择。但要记住一点,strstream虽仍然是C++语言标准的一部分,但已被C++标准宣称为“deprecated”,也就是不再提倡使用了,也说不定以后干粹就没了。先介绍一下sstream
//stringstream流以空格为边界符,使用其须包含sstream头文件
//istringstream 用法
istringstream istring;
string ss("ss 8346520");
istring.str(ss);
int i=0;
string s;
istring>>s>>i;
cout<<s<<" "<<i<<endl;
或者
istringstream istring("ss 8346520");
int i=0;
string s;
istring>>s>>i;
cout<<s<<" "<<i<<endl;
都将打印 s内容是ss,i内容是8346520的结果;

//ostringstream 用法
string s="test";
int i=8346520;
int j=0;
string s1;
ostringstream ostring;//不能写成ostringstream ostring<<s<<" "<<i;
ostring<<s<<" "<<i;
cout<<ostring.str()<<endl;//ostring流内保存内容是 test 8346520
istringstream istring(ostring.str());
istring>>s1>>j;//要注意此处的顺序;
cout<<s1<<"――――"<<j<<endl;

简单说说strstream:
基于数组的类有istrstream、ostrstream和strstream。它们分别用来创建输入、输出和输
入/输出流。这些类的基类之一是strstreambuf,它定义了派生类使用的几个底层的具体属
性。
除了strstreambuf以外,istream 也是istrstream的基类。类ostrstream包括了类ostream。
strstream也包括了类iostream。所以,所有基于数组的类和“普通”I/O类一样存取相同的成
员函数。
创建基于数组的输出流
要将一个输出流和一个数组关联起来,可使用下列ostream的构造函数:
ostrstream ostr(char *buf, int size, int mode=ios::out);
其中,buf是指向数组的指针,该数组接收写入流的字符。数组的长度由参数size确定。缺省
时,流以输出方式打开,但也可以将几项或在一起复合为所需的方式(例如,可以包含ios::
app使输出添加在数组中已存在的信息的尾部)。mode的缺省值可以满足大多数的要求。
一旦打开了一个基于数组的输出流,所有对这个流的输出就放在数组中。但是,任何输
出都不能写到数组的限界之外,任何这种企图都会导致错误。
下面是一个介绍基于数组的输出流的简单程序。
#include <iostream>
#include <strstream>
using namespace std;
int main()
{
int arraysize=50;
char *pbuffer=new char[arraysize];
ostrstream ostr(pbuffer,arraysize,ios::out);
ostr<<"Hello"<<" ";
ostr<<99-14<<hex<<" ";
ostr.setf(ios::showbase);
ostr<<100<<ends; //使用ostrstream输出到流对象的时候,要用ends结束字符串
cout<<pbuffer;
delete[] pbuffer;
return 0;
}

使用数组作输入:
要将输入流和数组关联起来,可使用下列istrstream的构造函数:
istrstream istr(char*buf);
其中,buf是指向数组的指针,该数组作为每次向流输入的字符源。 buf所指的数组必须以空
结束。空结束符从不从数组中读取。
下面是一个用字符串输入的例子:
#include <iostream>
#include <strstream>
using namespace std;
int main()
{

const char s[]="10 Hello 15 12.23 done";
istrstream ins(s);
int i;
char str[80];
float f;
//reading: 10 Hello
ins >>i;ins >>str;
cout<<i<<" "<<str<<endl;
// reading:f 12.23 done.
ins>>i;
ins>>f;
ins>>str;
cout<<hex<<i<<" "<<f<<" "<<str;
return 0;
}

最后是文件i/o流:
//ifstream 用法 须包含fstream头文件
ifstream infile("in.txt");//或者ifstream infile;infile.open("1.txt");
infile.close();//fstream对象只能与1个文件关联,所以打开另外一个,必须关闭上一个;
infile.clear();//如果是循环创建fstream流的话,.close()和clear()函数就应该重视;
infile.open("d:/in1.txt");//文件夹地址应使用反斜杠;
if(infile){//使用前检测状态是个好习惯
string s;
infile>>s;//将读取文件里第1个单词;getline(infile,s);将读取文件里的第1行
cout<<s<<endl;
}

//ofstream 用法
这里涉及很多文件模式,如下:
in――――――――――打开文件做读操作
out――――――――打开文件做写操作
app――――――-添加模式:在每次写之前找到文件尾
ate――――――――打开文件后立即将文件定位在文件尾
trunc――――-打开文件时清空已存在的文件流
binary――――以二进制模式进行IO操作
out,trunc,app只能用于ofstream和fstream对象关联的文件;
in模式只使用于ifstream和fstream对象关联的文件
所有的文件都可以用ate或binary模式打开;
默认时:ifsteam对象关联的文件以in模式打开;ofstream对象关联的文件以out模式打开,以out模式打开的文件会被清空;
string file="d:/out.txt";
ofstream outfile(file.c_str());//默认模式
outfile<<"hello world\n";
outfile.close();//注意close;
outfile.clear();
outfile.open(file.c_str(),ofstream::app);//显式更改文件模式,添加模式
outfile<<"\nhello world again!";
  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值