#include <fstream> #include <iostream> #include <string.h> #define BUF_SIZE 256 using namespace std; int main(){ //ifstream fromfiletobuf;//输入方式,为从文件输入到buf ifstream fromfiletobuf("infilename.txt",ios::in); //ifstream fromfiletobuf("infilename.txt"); ofstream frombuftofile;//输出方式,为从buf输出到文件 //ofstream frombuftofile("outfilename.txt",ios::out|ios::app); //ofstream frombuftofile("outfilename.txt"); char buf[BUF_SIZE];//声明buf /* 打开文件,open函数原型void open(const char *pathname,int mode) pathnme指向要打开的文件路径的字符串(包括文件名) mode常用的取值 ios::app追加模式 ios::ate定位到文件尾 ios::binary以二进制模式打开 ios::in以输入方式打开 ios::out以输出方式打开 ios::trunc若要打开的文件存在,则把文件长度设为0 这些标识符可以被组合使用,中间以”或”操作符(|)间隔 例如:ofstream file; file.open("filename.txt",ios::out|ios::binary|ios::app); ofstream, ifstream 和 fstream所有这些类的成员函数open 都包含了一个默认打开文件的方式,这三个类的默认方式各不相同: 类 参数的默认方式 ofstream ios::out | ios::trunc ifstream ios::in fstream ios::in | ios::out 只有当函数被调用时没有声明方式参数的情况下,默认值才会被采用。如果函数被调用时声明了任何参数,默认值将被完全改写,而不会与调用参数组合。 */ //fromfiletobuf.open("infilename.txt"); frombuftofile.open("outfilename.txt",ios::out); /* 可以通过调用成员函数is_open()来检查一个文件是否已经被顺利的打开了: bool is_open(); 它返回一个布尔(bool)值,为真(true)代表文件已经被顺利打开,假( false )则相反。 */ if(!fromfiletobuf.is_open()){ cerr<<"can't open file to read"<<endl; return -1; } if(!frombuftofile){ cerr<<"can't open file to write"<<endl; return -1; } /* getline用于读取整行文本。 istream getline(char *buf, int buf_size, char delimiter='/n') istream getline(ifstream cin, string str,char delimiter='/n'); string str; while(getline(fromfiletobuf,str)){ cout<<str<<endl; frombuftofile<<str<<endl; } */ int readin=0; while(!fromfiletobuf.eof()){ fromfiletobuf.getline(buf,BUF_SIZE); /*gcount()实际读入的字符数*/ readin=fromfiletobuf.gcount(); cout<<"we read "<<readin<<" bytes"<<endl; cout<<buf<<endl; frombuftofile<<buf<<endl; } /* 状态标志符,iostream 有四个可用的状态标志符,分别为: ios::badbit 当执行了无效操作时,badbit位为1 ios::eofbit 当遇到文件结束符的时候,eofbit为1 ios::failbit 当打开文件流对象失败或遇到无效输入格式时 failbit为1 ios::goodbit 当badbit、eofbit、failbit为 0 时, goodbit为1 对应的有四个状态标志符验证函数,分别为: bool bad()、bool eof()、bool fail()、bool good() setstate()函数,在现有的状态基础上在增加一个条件,例如:本身eofbit的值为1 当调用fromfiletobuf.setstate(ios::badbit)后,eofbit的值还为1,同时 badbit的值也为1,fromfiletobuf.clear(ios::badbit),将badbit的值设 为1,同时eofbit的值也被设为0 setstate()函数可用按位操作符,例如: fromfiletobuf.setstate(ifstream::badbit|ifstream::failbit); 将badbit和failbit都设置为1 rdstate()函数能够显式的访问iostream(fstream)类对象的状态 */ cout<<"fromfiletobuf.bad():"<<fromfiletobuf.bad()<<endl <<"fromfiletobuf.eof():"<<fromfiletobuf.eof()<<endl <<"fromfiletobuf.fail():"<<fromfiletobuf.fail()<<endl <<"fromfiletobuf.good():"<<fromfiletobuf.good()<<endl <<"fromfiletobut.rdstate(): "<<fromfiletobuf.rdstate()<<endl; /* fromfiletobuf.clear()设置之后只有goodbit为1,其余为0 clear()却省为clear(ios::goodbit) */ fromfiletobuf.clear(); fromfiletobuf.clear(ios::badbit); cout<<"after fromfiletobuf.clear(ios::badbit)"<<endl; cout<<"fromfiletobuf.bad():"<<fromfiletobuf.bad()<<endl <<"fromfiletobuf.eof():"<<fromfiletobuf.eof()<<endl <<"fromfiletobuf.fail():"<<fromfiletobuf.fail()<<endl <<"fromfiletobuf.good():"<<fromfiletobuf.good()<<endl <<"fromfiletobut.rdstate(): "<<fromfiletobuf.rdstate()<<endl; fromfiletobuf.clear(); fromfiletobuf.setstate(ios::eofbit); cout<<"after fromfiletobuf.setstate(ios::eofbit)"<<endl; cout<<"fromfiletobuf.bad():"<<fromfiletobuf.bad()<<endl <<"fromfiletobuf.eof():"<<fromfiletobuf.eof()<<endl <<"fromfiletobuf.fail():"<<fromfiletobuf.fail()<<endl <<"fromfiletobuf.good():"<<fromfiletobuf.good()<<endl <<"fromfiletobut.rdstate(): "<<fromfiletobuf.rdstate()<<endl; fromfiletobuf.clear(); cout<<"after fromfiletobuf.clear()"<<endl; cout<<"fromfiletobuf.bad():"<<fromfiletobuf.bad()<<endl <<"fromfiletobuf.eof():"<<fromfiletobuf.eof()<<endl <<"fromfiletobuf.fail():"<<fromfiletobuf.fail()<<endl <<"fromfiletobuf.good():"<<fromfiletobuf.good()<<endl <<"fromfiletobut.rdstate(): "<<fromfiletobuf.rdstate()<<endl; /* 获得和设置流指针 tellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数, 代表当前get流指针的位置 (用tellg) 或 put 流指针的位置(用tellp). seekg()和seekp() 这对函数分别用来改变流指针get 和put的位置。两个函数都被重载为两种不同的原型: seekg ( pos_type position ); seekp ( pos_type position ); 使用这个原型,流指针被改变为指向从文件开始计算的一个绝对位置。 要求传入的参数类型与函数 tellg 和tellp 的返回值类型相同。 seekg ( off_type offset, seekdir direction ); seekp ( off_type offset, seekdir direction ); 使用这个原型可以指定由参数direction决定的一个具体的指针开始计算的一个位移(offset)。它可以是: ios::beg 从流开始位置计算的位移 ios::cur 从流指针当前位置开始计算的位移 ios::end 从流末尾处开始计算的位移 */ string str; fromfiletobuf.seekg(3,ios::beg); cout<<"fromfiletobuf.tellg(): "<<fromfiletobuf.tellg()<<endl; getline(fromfiletobuf,str); cout<<"str: "<<str<<end; frombuftofile.seekp(15,ios::beg); cout<<"frombuftofile.tellp(): "<<frombuftofile.tellp()<<endl; frombuftofile<<str<<endl; //关闭文件 fromfiletobuf.close(); frombuftofile.close(); return 0; } 另外这篇 http://hi.baidu.com/xpayt/blog/item/9a2b3a3033f5cc9ca9018e8f.html 讲的也比较全面, 本程序有借鉴这篇介绍。