* (1)ofstream : 写文件 * (2)ifstream : 读文件 * (3)fsream : 读写文件
------------------------------------------------------
#include "iostream" #include "iomanip" #include "fstream"
1.写文件(向 aa.txt 写入数据) ofstream f1("d:\\aa.txt"); if(!f1){ return 0; } //写入文件 setw f1<<setw(20)<<"姓名:"<<"sam.dong"<<endl; f1<<setw(20)<<"年龄:"<<"22"<<endl; f1<<setw(20)<<"地址:"<<"中国.广东.珠海"<<endl; f1<<setw(20)<<"手机号:"<<"13722222222"<<endl; f1.close();
2.读取文件数据
char data[200]; ifstream f; cout<<"Processing file"<<endl; f.open("d://aa.txt",ios::in|ios::binary); if(!f.is_open()){ cout<<"Cannot open"<<endl; } while(f.getline(data, 100)){ cout<<data<<endl; } f.close();