#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <string>
usingnamespace std;
struct reserve
{
string name;
string date;
string time;
string operation;
};
int main()
{
reserve res;
fstream myfile;
char choice;
myfile.open("Reservation.txt",ios::in|ios::out|ios::app);
choices:
cout<<"\t\t\tReservation"<<endl;
cout<<"Choice the letter of your choice:"<<endl;
cout<<"[A]. View Reservation\n[B]. Add Reservation\n[C]. Remove Reservation\n[D]. Exit Program"<<endl;
cout<<"\nChoice:";
cin>>choice;
cin.ignore(10000, '\n');
if (choice=='A'||choice=='a')
{
system("cls");
string output;
myfile.seekg(0,ios::beg);
cout<<endl<<endl;
cout<<left<<setw(25)<<"NAME";
cout<<left<<setw(25)<<"DATE";
cout<<left<<setw(20)<<"TIME";
cout<<left<<setw(20)<<"OPERATION";
cout<<"\n";
while (getline(myfile, output))
{
cout<<endl;
cout<<left<<setw(25)<<output;
std::getline(myfile, output);
cout<<left<<setw(25)<<output;
std::getline(myfile, output);
cout<<left<<setw(20)<< output;
std::getline(myfile, output);
cout<<left<<setw(20) << output;
cout<<endl;
}
cout<<endl<<endl;
system("pause");
system("cls");
cout<<"\n\n\n \t\t\t\tT H A N K Y O U! \n\n\n"<<endl;
system("pause");
}
elseif(choice=='B'||choice=='b')
{
cout<<"\n\tEnter the name:";
getline(cin,res.name);
cout<<"\n\tEnter the date:";
getline(cin,res.date);
cout<<"\nEnter the time:";
getline(cin,res.time);
cout<<"\n\tEnter the operation:";
getline(cin,res.operation);
myfile<<res.name<<"\n"<<res.date<<"\n"<<res.time<<"\n"<<res.operation<<"\n";
system("cls");
cout<<"\n\n\n\t\t\t\tS U C C E S S F U L L Y A D D E D\n\n\n"<<endl;
system("pause");
system("cls");
goto choices;
}
elseif(choice=='C'||choice=='c')
{
}
elseif(choice=='D'||choice=='d')
{
return 0;
}
else
{
system("cls");
goto choices;
}
}
If you're reading, then open for reading, then read and then close.
If you're writing, then open for writing, then write and then close.
If you're deleting records, then open the existing file for reading, open a new file for writing and then copy all the records you want to keep.
In other words, keep it simple (KISS).
If you try to do everything with a single open file, you're going to come unstuck.
all you need is this, with the supporting code around it (variable creation etc). This is pseudocode!
while ( data = file.getline)
{
if (data != getridofme)
outfile << data; //implied else, do not write the bad line to the file.
}
if the bad data is not a full line you have to break it down more but that is the LOGIC you need.
The process behind the logic is to read the file into memory of the computer, change that, and write it back out into a new file with the updated contents.
Start there. Get this working. Once you understand it, then you can try something a little bigger like reading the original file into a container, updating it, and writing it back over the original file. And later we can talk about files that are too big to dump into memory all at once, but those are going to be many gigabytes in size on even a modest PC, and you can ignore this issue while learning the basics.
Modify the file is really modifying your data storage of the file, in other words. Say you put each 'word' into a list of strings, then you can remove some of the words from the list, then you can write it all back out again with the missing stuff gone. This is what you need to understand; you don't really delete data from a file 'in place' on the disk. Its a lot like how you delete an element from the middle of an array: you have to move all the data after the deleted bit towards the start, to close the gap(s) between the deleted data and the kept data.
I know you are asking for code but code without understanding these ideas is useless, and these ideas are simple to express in code once you understand them.