c++ IO的设计保证IO效率,同时又兼顾封装性和易用性。本文将会讲述c++文件流的用法。
有错误和疏漏的地方,欢迎批评指证。
需要包含的头文件: <fstream>
名字空间: std
也可以试用<fstream.h>
fstream提供了三个类,用来实现c++对文件的操作。(文件的创建,读写)。
ifstream -- 从已有的文件读
ofstream-- 向文件写内容
fstream- 打开文件供读写
支持的文件类型
实际上,文件类型可以分为两种: 文本文件和二进制文件.
文本文件保存的是可读的字符, 而二进制文件保存的只是二进制数据。利用二进制模式,你可以操作图像等文件。用文本模式,你只能读写文本文件。否则会报错。
例一: 写文件
声明一个ostream变量
可以像试用cout一样试用操作符<<向文件写内容.Usages:
例二: 读文件
1. 声明一个ifstream变量.
2. 打开文件.
3. 从文件读数据
4. 关闭文件.
1.同样的,你也可以使用构造函数开打开一个文件、你只要把文件名作为构造函数的
第一个参数就可以了。
上面所讲的ofstream和ifstream只能进行读或是写,而fstream则同时提供读写的功能。voidmain()
open函数的参数定义了文件的打开模式。总共有如下模式
Notes
我们也可以调用read函数和write函数来读写文件。
文件指针位置在c++中的用法:
常用的错误判断方法:
在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!"; |