64,683
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc,char **argv)
{
char create;
ofstream os;
string id,name;
os.open("xx.dat",ios::in);
if(!os)
{
cout<<"你想创建新帐户吗?\n";
cin>>create;
if(create == 'y')
{
os.open("xx.dat",ios::app);
cout<<"请输入你的ID及名字:\n";
cin>>id>>name;
os<<id<<"|"<<name;
}
else
{
exit(0);
}
}
os.close();
ios::app: 以追加的方式打开文件
ios::ate: 文件打开后定位到文件尾,
ios:app就包含有此属性
ios::binary: 以二进制方式打开文件,缺省的方式是文本方式。两种方式的区别见前文
ios::in: 文件以输入方式打开 (也就是读方式)
ios::out: 文件以输出方式打开 (这个才是写)
ios::nocreate: 不建立文件,所以文件不存在时打开失败
ios::noreplace:不覆盖文件,所以打开文件时如果文件存在失败
ios::trunc: 如果文件存在,把文件长度设为0