Reputation: 103
Why is my file 'database2' empty and no files being renamed? After the part where the problem is stated in comments, the file 'database2' becomes 0 bytes. The file 'database2' is also not renamed and the file 'database' is not being deleted. Please help me, here is part of my code:
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;
Upvotes: 0
Views: 507
Reputation: 2435
This is whats happening:
You have a "loop" going on with the "start" label and the goto at the end. So this will be the first part of the loop:
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);
See in the first part of your loop you open/create two files called "database" and "database2" (note that ofstream open method creates the file if it doesnt exist already) and then waits for the user input.
Lets say the user press 1 to change the name, at the end of the case 1 statement you have this:
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;
You close the files, delete database, and rename databese2 to database, which is working as intended, and then you go back to the start of the "loop" with the goto, which excecutes this part of the code again:
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");
You open database file and since you renamed your old database2 to database (database2 now doesnt exist) it creates a new one (empty of course).
hope it helps.
Upvotes: 3