文件中的错误已存在

Error in file already exists

本文关键字:存在 错误 文件      更新时间:2023-10-16

在检查文件是否已存在后,我正在尝试为帐户创建文件! 但是我卡住了,因为我遇到了意外的运行时错误!

第一次运行

输入帐户名称 : 哈桑

已创建帐户

第二次运行

输入帐户名称:哈桑

帐户已存在

输入帐户名称 : 哈桑

已创建帐户

这就是问题所在!!它创建名为"assan"的文件

法典

/*
********************
1.  ADD ACCOUNT
********************
*/
void add_account(){
system("cls");
cout<<"nt******************************************n";
cout<<"ttADD ACCOUNT MENUn";
cout<<"t******************************************n";

//Taking account name
again:
Account new_account;
cout<<"nntPlease Enter the name of account : ";
cin.ignore(); //for clearing buffer
cin.getline(new_account.account_name,79);
if(create_file_for_account(new_account.account_name)==0)
    goto again;

cout<<endl<<endl; system("pause");
return;
}
/*
********************
1(a). CREATE FILE OF ACCOUNT
********************
*/
int create_file_for_account(char file_name[])
{
//Check if file exists already
if(does_file_exist(file_name)){
    cout<<"nnSorry, account already exists!";
    return 0;
}
ofstream account;
account.open(file_name,ios::out);
//Check if file created successfully
if(account.good()){
    cout<<"File created";
    return true;
}
else{
    return false;
}
}
/*
********************
1(b). CHECK IF FILE EXISTS
********************
*/
bool does_file_exist(char file_name[])
{
ifstream check(file_name, ios::ate);
if(check)
    return true;
else{
    return false;
}
}

请帮忙,我花了几个小时来捕捉错误,但我不能由于我是初学者C++ :p请...

实际上,"cin.ignore"将仅丢弃输入的第一个字符(如果有的话)(见 http://www.cplusplus.com/reference/istream/istream/ignore/)

所以我假设"H"不知何故被忽略了......