为什么我的档案是空的?
Why is my file empty?
为什么我的文件'database2'为空,没有文件被重命名?在注释中说明问题的部分之后,文件'database2'变为0字节。文件'database2'也没有重命名,文件'database'也没有被删除。请帮助我,这是我的代码的一部分:
int edit(){
char KeyStroke;
std::string strings;
string name;
string name1;
std::string dummy;
std::string strings2;
std::string strings3;
ifstream myfile;
ofstream myfile1;
bool found = false;
myfile.open("database");
cout << endl << "What is the name of the contact you wish to go in detail?" << endl;
getline(cin, name);
while ( !found && getline (myfile ,strings) ) {
if (strings.find(name) != std::string::npos) {
cout << endl << "Name:tt" << strings<<endl;
std::getline( myfile, strings );
cout << "Address:t" << strings<<endl;
std::getline( myfile, strings );
cout << "Handphone:t" << strings;
}
}
start:
cout << endl <<endl;
cout << "What do you wish to edit?"<<endl;
cout << "1) Name"<<endl;
cout << "2) Address"<<endl;
cout << "3) Handphone"<<endl;
cout << "4) Nothing" << endl;
myfile.close();
/*--Main part of this code is from here onwards--*/
lol:
myfile.open("database");
myfile1.open("database2");
KeyStroke = getch();
switch(KeyStroke){
case '1':
cout << "What is the new name: ";
getline(cin, name1);
while ( !myfile.eof()) {
getline (myfile ,strings);
if (strings.find(name) != std::string::npos) {
myfile1 << name1 << endl;
}
else{
if(strings[0] == ' '){
continue;
}
myfile1 << strings << endl;
}
}
myfile1 << " ";
myfile1.close(); /*Once the file closes here, the data written in earlier dissapears*/
myfile.close();
remove("database");
pause1();
rename("database2","database");
goto start;
事情是这样的:
你有一个"循环"与"开始"标签和结束的goto。所以这将是循环的第一部分:
cout << endl <<endl;
cout << "What do you wish to edit?"<<endl;
cout << "1) Name"<<endl;
cout << "2) Address"<<endl;
cout << "3) Handphone"<<endl;
cout << "4) Nothing" << endl;
myfile.close();
/*--Main part of this code is from here onwards--*/
lol:
myfile.open("database");
myfile1.open("database2");
cin.get (&KeyStroke,256);
看到在你的循环的第一部分,你打开/创建两个文件名为"database"answers"database2"(注意,ofstream打开方法创建的文件,如果它不存在),然后等待用户输入。
让我们假设用户按1来更改名称,在case 1语句的末尾,你有这样的:
myfile1 << " ";
myfile1.close(); /*Once the file closes here, the data written in earlier dissapears*/
myfile.close();
remove("database");
// pause1(); // undefined
rename("database2","database");
goto start;
关闭文件,删除数据库,并将databese2重命名为database,这是预期的工作,然后使用goto返回到"循环"的开始,它再次执行这部分代码:
cout << endl <<endl;
cout << "What do you wish to edit?"<<endl;
cout << "1) Name"<<endl;
cout << "2) Address"<<endl;
cout << "3) Handphone"<<endl;
cout << "4) Nothing" << endl;
myfile.close();
lol:
myfile.open("database");
myfile1.open("database2");
你打开数据库文件,因为你把旧的database2重命名为database (database2现在不存在),它创建了一个新的(当然是空的)。
希望能有所帮助。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我的字符计数代码计算错误.为什么
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- cmake在我的项目中所需的所有静态库都不成功
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 为什么我的for循环不能正确获取argv
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 0-1背包代码中的错误.我的代码中有什么错误
- 当我的阵列太大时出现分段错误
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 为什么二进制搜索在我的测试中不起作用
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- OpenGL在启用深度测试时不会丢弃我的碎片
- 为什么我的 std::ref 无法按预期工作?
- 我的档案里什么都没写
- 为什么我的档案是空的?