I have a program that reads and stores information, however, when you store information, it just overwrites what it had before, so some of the previous information remain. Is there any possibility to clear (erase everything) from a text file?
void hotel::removeCustomer(string nameReg)
{
fstream readData ; // Reads data from file
readData.open ("information.dat");
fstream printData; // Prints data to file
printData.open ("information.dat", ios::trunc);
int accountNum;
if(readData.fail() == true){ // This will check if the file exists or not
cout << "Error: cannot find file!" << endl;
system("PAUSE");}
readData >> i; // Reads the first number
i = i - 1;
numStore.push(i); // Stores number i in stack
getline(readData,line); // Skip to next line
for (int j = 0; j < i+1; j++) // Stores data of each line into stacks
{
readData >> nameOfCustomer >> account;
nameStore.push(nameOfCustomer);
accountStore.push(account);
getline(readData,line);
}
printData << numStore.top() << "\n"; // Print number i into file
for (int k = 0; k < i+1; k++)
{
if(nameStore.top() != nameReg) // If the name at top of stack does not equal input
{
printData << nameStore.top() << " " << accountStore.top() << "\n"; // Print data into file
nameStore.pop(); // Pop the top
accountStore.pop(); // Pop the top
}
else // If top == input
{
nameStore.pop(); // Pop the top
accountStore.pop(); // Pop the top
}
}
}
1 2 3 4 5 6 7
int main()
{
hotel registerD;
string removeName;
cin >> removeName;
registerD.removeCustomer(removeName);
}
Contents in file (information.dat):
5
John 1728
James 6621
Jerry 7121
Jessica 8881
Jean 2712
void hotel::removeCustomer(string nameReg)
{
fstream readData ; // Reads data from file
if( !readData.open( "information.dat" , ios::in ) )
{
cout<<"couldn't open the file\n";
exit(1);
}
//found and open file correctly
int i;
readData >> i; // Reads the first number
numStore.push(--i); // Stores number i in stack
//i think you dont need to change to next line
//because there is nothing else to read from the 1st line
//getline(readData,line); // Skip to next line
while( readData )//when it reaach EOF it will continue after the while statement
{
readData >> nameOfCustomer >> account;
nameStore.push(nameOfCustomer);
accountStore.push(account);
//you might need the next line inorder to skip the end of line character
//readData.get();
}
//and now that you have read all the file you can close it and open it again
// this time with ios::trunc mode
readData.close();
if( !readData.open( "information.dat" , ios::out | ios::trunc ) )
{
cout<<"couldn't open the file\n";
exit(1);
}
//so now you have an empty file and everything saved in the stack
for (int k = 0; k < i+1; k++)
{
if(nameStore.top() != nameReg) // If the name at top of stack does not equal input
{
readData << nameStore.top() << " " << accountStore.top() << "\n"; // Print data into file
nameStore.pop(); // Pop the top
accountStore.pop(); // Pop the top
}
else // If top == input
{
nameStore.pop(); // Pop the top
accountStore.pop(); // Pop the top
}
}
}
i didnt compile it but the main problem i think was that you were trying to read from an empty file.
also by using a stask to save your data, your new file will have the oposite order in names
you can use a FIFO queue instead.
Okay, I removed the file checking and changed the while loop back to for loop because it was not working right. Now everything works perfectly fine after those changes. I can see where I made my mistakes before in my first attempt. I did not close the files and whatnot.