C++文件输入输出

来源:互联网 发布:苹果mac切换输入法 编辑:程序博客网 时间:2024/05/22 15:21

保存在变量,数组,对象中的数据都是暂时性的,当程序退出以后就会丢失,为了永久性保存程序中有用数据,需要将数据以文件形式保存于磁盘或者光盘等物理介质。C++文件主要是两种:文本文件和二进制文件:例如C++源程序是存储在文本文件,而最后的可执行程序是二进制文件。当然计算机本身是不区分二进制文件和文本的,文件实际物理存储统一都是二进制格式,也即文本IO也是基于二进制IO的,只不过在此之上提供了一层字符编解码抽象。C++里主要定义了ifstream,ofstream,fstream用于处理和操作文件,这些类都定义在<fstream>中。

1 :文本文件的输入输出

文本文件的输入输出主要涉及<<插入和>>流抽取运算符,例如文件输入,涉及的是从磁盘读取数据到内存,即从输入流对象读入数据:

int main(){ifstream in;in.open("D:/score.txt");if(in.fail()){cout<<"the file does not exist!"<<endl;exit(0);}char name[80];char t;char fam[80];int a;while(!in.eof()){in>>name>>t>>fam>>a;cout<<name<<" "<<t<<" "<<fam<<" "<<a<<endl;}in.close();cout<<"done"<<endl;system("pause");return 0;}
我们看到类似标准输入cin,只不过cin是针对控制台,而文件类in针对的是文件;类似的还有文本文件的输出,即从内存到磁盘,是把数据写入到输出流:

int main(){ofstream out;out.open("D:/score.txt");string name("yanshiwei");out<<"yanshiwie"<<" "<<'T'<<" "<<"Smith"<<":"<<90<<endl;out<<"jianghe"<<" "<<'K'<<" "<<"Jone"<<":"<<89;out.close();cout<<"done"<<endl;system("pause");return 0;}

类似cout,只不过cout是针对屏幕,文件类out针对的是磁盘D里的txt文件.

不过,>>方式类似cin一样需要面对一个问题就是读取数据时候,以空白为结束,这样对于含有空白的一段字符串来说,提取不完全的。所以文本文件操作除了<<,>>外还有get(从输入流里读取数据)和put(写入数据到输入流):get()有三种版本:char get();    istream *get(char &ch);  char get(char a[],int size,char delimitchar)---类似于getline(),差别在于getline()会把空结束符‘\0’放置在字符数组。put()则只有一种版本:void put(char ch):

int main(){const int s=40;char source[s];char dest[s];cout<<"enter the source file name:"<<endl;cin>>source;cout<<"enter the dest file name:"<<endl;cin>>dest;ifstream in(source);ofstream out(dest);if(in.fail()){cout<<"the source file does not exist"<<endl;exit(0);}char ch=in.get();while(!in.eof()){out.put(ch);ch=in.get();/*out.put(in.get());*/}in.close();out.close();cout<<"done"<<endl;system("pause");return 0;};

在上面代码已经看到eof(),fail()等检查流状态的函数,实际上每个流都包含一个位集合,起到标示位作用,这些位置0,1指明了流状态:

ios::eofbit                                   当前文件末尾置位                    由ios::eof()函数获得,置位则返回true

ios::failbit                                   操作失败时置位                         由ios::fail()获得

ios::hardbit                                发生不可恢复错误时置位         由ios::fail()获得

ios::badbit                                 试图非法操作时置位                  由ios::bad()获得

ios::goodbit                               操作成功置位                              由ios::good()获得

inline void statebit(fstream &s){cout<<"the state bit of current bit:"<<endl;cout<<"eof: "<<s.eof()<<endl;cout<<"failbit: "<<s.fail()<<endl;cout<<"badbit: "<<s.bad()<<endl;cout<<"goodbit: "<<s.good()<<endl;}int main(){fstream s;s.open("D:/file.txt",ios::out);s<<"Dallas";cout<<"Normal operation no error"<<endl;statebit(s);//show the statebit of current streams.close();s.open("D:/file.txt",ios::in);if(s.fail()){cout<<"the file no"<<endl;exit(0);}char a[22];while(!s.eof()){s>>a;cout<<a;}cout<<"end of file"<<endl;statebit(s);s.close();s>>a;cout<<s;statebit(s);system("pause");return 0;}

2:二进制文件输入输出

二进制文件的输入输出主要read()和write()函数为主,这里主要介绍的也是这两个函数。

streamobj.write(char *add,int size)//从内存写入磁盘

streamobj.read(char *add,int size)//从磁盘读入内存

我们看到主要针对的是字符串,当数据是普通数据类型,数组,对象时候,C++提供了reinterpret_cast<char *>(add)转换:

下面是普通数据文件的二进制文件操作:

int main(){fstream f;f.open("D:/file.dat",ios::out|ios::binary);;char s[]="Atlanta";char s1[22];int b;f.write(s,5);f.close();f.open("D:/file.dat",ios::out|ios::binary|ios::app);int a=100;f.write(reinterpret_cast<char *>(&a),sizeof(a));f.close();f.open("D:/file.dat",ios::in|ios::binary);f.read(s1,5);s1[5]='\0';cout<<s1<<endl;f.read(reinterpret_cast<char *>(&b),sizeof(b));cout<<b<<endl;f.close();cout<<"done"<<endl;system("pause");return 0;}//二进制文件普通数据IO

下面是普通数组文件的二进制文件操作:

int main(){fstream obj;char s[]="shanghai";char s1[22];int a[]={22,11,22,11};int b[4];obj.open("D:/file.dat",ios::out|ios::binary);obj.write(s,sizeof(s));obj.write(reinterpret_cast<char *>(a),sizeof(a));obj.close();obj.open("D:/file.dat",ios::in|ios::binary);obj.read(s1,sizeof(s));cout<<s1<<endl;obj.read(reinterpret_cast<char *>(b),sizeof(b));for(int i=0;i<4;i++)cout<<b[i]<<" ";obj.close();system("pause");return 0;}//二进制文件数组的IO
下面是对象的文件的二进制文件操作:
inline void dis(stu&s){cout<<"the inf:"<<endl;cout<<"First name "<<" Mid name "<<" Last name "<<" The score "<<endl;cout<<setw(6)<<s.getfname()<<setw(10)<<s.getmid()<<setw(13)<<s.lname()<<setw(10)<<s.getscore()<<endl;cout<<endl;}int main(){stu stud[5];stu stu1[5];int i=0;for(i=0;i<5;i++)   stud[i]=stu("Jone",'K',"Smith",80+i);fstream ob;ob.open("D:data.dat",ios::out|ios::binary);for(i=0;i<5;i++){ob.write(reinterpret_cast<char *>(&stud[i]),sizeof(stu));}ob.close();ob.open("D:data.dat",ios::in|ios::binary);for(i=0;i<5;i++){ob.read(reinterpret_cast<char *>(&stu1[i]),sizeof(stu));dis(stu1[i]);}ob.close();ob.open("D:/data.dat",ios::in|ios::out|ios::binary);ob.seekg(2*72L,ios::beg);ob.read(reinterpret_cast<char *>(&stu1[0]),sizeof(stu));cout<<"old:"<<endl;dis(stu1[0]);stu1[0].setfname("change");ob.seekp(2*72L,ios::beg);ob.write(reinterpret_cast<char *>(&stu1[0]),sizeof(stu));ob.seekg(2*72L,ios::beg);ob.read(reinterpret_cast<char *>(&stu1[0]),sizeof(stu));cout<<"new:"<<endl;dis(stu1[0]);ob.close();system("pause");return 0;}/*对象时二进制文件操作*/

附:利用seekg(),seegp()函数可以实现随机访问文件。

最后仅贴最后一个程序运行结果:


0 0
原创粉丝点击