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

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

2015-11-04 17:47 465 查看




前言:

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

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

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

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

名字空间: std

也可以试用<fstream.h>

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

ifstream -- 从已有的文件读

ofstream-- 向文件写内容

fstream- 打开文件供读写

支持的文件类型

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

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

例一: 写文件

声明一个ostream变量

调用open方法,使其与一个文件关联

写文件

调用close方法.



#include <fstream.h>

void main

{

ofstream file;

file.open("file.txt");

file<<"Hello file/n"<<75;

file.close();

}

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



file<<"string/n";

file.put('c');

例二: 读文件

1. 声明一个ifstream变量.

2. 打开文件.

3. 从文件读数据

4. 关闭文件.



#include <fstream.h>

void main

{

ifstream file;

char output[100];

int x;

file.open("file.txt");

file>>output;

cout<<output;

file>>x;

cout<<x;

file.close();

}

同样的,你也可以像cin一样使用>>来操作文件。或者是调用成员函数
Usages:



file>>char *;

file>>char;

file.get(char);

file.get(char *,int);

file.getline(char *,int sz);

file.getline(char *,int sz,char eol);

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

第一个参数就可以了。



ofstream file("fl.txt");

ifstream file("fl.txt");

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



{

fstream file;

file.open("file.ext",iso::in|ios::out)

//do an input or output here

file.close();

}

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



属性列表

ios::in 读

ios::out 写

ios::app 从文件末尾开始写

ios::binary 二进制模式

ios::nocreate 打开一个文件时,如果文件不存在,不创建文件。

ios::noreplace 打开一个文件时,如果文件不存在,创建该文件

ios::trunc 打开一个文件,然后清空内容

ios::ate 打开一个文件时,将位置移动到文件尾

Notes

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

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

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



ios::beg 文件头

ios::end 文件尾

ios::cur 当前位置
例子:



file.seekg(0,ios::end);

int fl_sz = file.tellg();

file.seekg(0,ios::beg);

常用的错误判断方法:



good() 如果文件打开成功

bad() 打开文件时发生错误

eof() 到达文件尾
例子:



char ch;

ifstream file("kool.cpp",ios::in|ios::out);

if(file.good()) cout<<"The
file has been opened without problems;

else cout<<"An Error has happend on opening the file;

while(!file.eof())

{

file>>ch;

cout<<ch;

}

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!";


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: