小白教你学习C++(9)文件流操作

xiaoxiao2021-02-28  93

#include <iostream> using namespace std; void main() { char *fname ="c:/2.txt"; char *fname1 ="c:/2ttrtgg.txt"; ofstream fout(fname1,ios::app,iso::ate); if(!fout) { cout<<"如果打开失败"<<endl;] return; } ofstream fout(fname,ios::out); fout <<"hello world"<<endl; //wrint a file fout <<"hello world"<<endl; fout <<"1232424ld"<<endl; fout <<"hello world"<<endl; fout.close(); ifstream fin(fname); //一输入的形式打开文件 cout <<"hello..."<<endl; char ch; while(fin.get(ch)) { cout<<ch; } system(pause); return; } class teacher { public: private: int age; char name[]; teacher() { age=30; strcpy(name,"" teacher(int _age,char *_name) { int age; strcpy(name,_name); } void printf() { cout<<"name"<<name<<age<<"age"<,endl; } } } //二进制文件的打开和写入 void main() { char *fname1 ="c:/2ttrtgg.dat"; ofstream fout(fname1,ios::binary); if(!fout) { cout<<"如果打开失败"<<endl;] return; } teacher T1(31,t31"); teacher T2(32,"t32"); fout.write((char*)&t1,sizeof(t1)) fout.write((char*)&t2,sizeof(t2)); ifstream fin(fname); Teacher tmp; fin.read(char*)&%tmp,aizeof(Teacher)); tmp.printf(); fin.read(char*)&%tmp,aizeof(Teacher)); tmp.printf(); }
转载请注明原文地址: https://www.6miu.com/read-21098.html

最新回复(0)