文件中的错误已存在
Error in file already exists
在检查文件是否已存在后,我正在尝试为帐户创建文件! 但是我卡住了,因为我遇到了意外的运行时错误!
第一次运行
输入帐户名称 : 哈桑
已创建帐户
第二次运行
输入帐户名称:哈桑
帐户已存在
输入帐户名称 : 哈桑
已创建帐户
这就是问题所在!!它创建名为"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"不知何故被忽略了......
相关文章:
- 我正在尝试制作一个自平衡机器人,但编译时存在错误。我不知道如何解决它
- Visual Studio 2019在内置库中存在错误
- 以下 OpenCV 源代码中是否存在错误?(#ifdef 没有 #else)
- 在存在错误代码的情况下输出参数与 NRVO
- 为什么我的删除功能中存在错误
- 哪个编译器(如果有的话)在参数包扩展中存在错误
- 未知功能已经存在错误
- progam在std :: getline AM之后停止响应,代码中是否存在错误
- Netuseradd用户已经存在错误
- GCC 4.4.1是否存在错误,因为它不接受ctor初始值设定项中的注入类名
- Visual C++中的外部模板是否存在错误
- 类模板成员不存在错误
- 如果构造函数中存在错误,请停止进程
- purify中存在错误的内存泄漏
- C++中是否存在错误类型
- 来自 vtable 的未定义符号是否意味着接口和实现之间存在错误
- 为什么我遇到此代码的分段错误?此代码几乎没有运行,因此构造函数或复制构造函数中可能存在错误
- c ++ 11 参数包在 Apple LLVM 7.0.0 中存在错误行为,但适用于 GCC-5.1
- 文件输出中存在错误字符
- 在boost::mutex构造函数中同时写入同一内存区域可能存在错误