C++ 为什么我的文件是空的?
为什么我的文件“database2”为空且没有重命名文件?在注释中说明问题的部分之后,文件“database2”变为0字节。文件“database2”也未重命名,文件“database”也未被删除。请帮助我,这是我的部分代码:C++ 为什么我的文件是空的?,c++,ifstream,ofstream,C++,Ifstream,Ofstream,为什么我的文件“database2”为空且没有重命名文件?在注释中说明问题的部分之后,文件“database2”变为0字节。文件“database2”也未重命名,文件“database”也未被删除。请帮助我,这是我的部分代码: int edit(){ char KeyStroke; std::string strings; string name; string name1; std::string dummy; std::string strin
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:\t\t" << 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;
int-edit(){
字符击键;
std::字符串;
字符串名;
字符串名称1;
字符串伪码;
std::字符串2;
std::字符串3;
ifstreammyfile;
流myfile1;
bool-found=false;
打开(“数据库”);
这就是正在发生的事情:
你有一个“循环”,在“开始”标签上,在结尾有“转到”。因此,这将是循环的第一部分:
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);
cout事情就是这样发生的:
你有一个“循环”,在“开始”标签上,在结尾有“转到”。因此,这将是循环的第一部分:
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);
从外观上看,您是否可能忘记了文件扩展名。您的代码很难阅读,太紧凑。扩展名应该不会影响,文件与程序位于同一目录中,没有扩展名。请稍等,我将它分成几个部分。关于删除()
和重命名()
调用时,您应该检查错误结果。根据您的描述,我怀疑您查找的目录不正确,或者存在访问权限问题。顺便说一句:请不要使用goto
,用循环结构替换它。从外观上看,您可能忘记了文件扩展名。您的代码很难阅读,太难理解compact.Extension应该不会影响,该文件与程序位于同一目录中,没有扩展名。请稍等,我将它分成几个部分。关于remove()
和rename())
调用时,您应该检查错误结果。根据您的描述,我怀疑您查找的目录不正确,或者存在访问权限问题。顺便说一句:请不要使用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");