editing saved records in a text file
hie evry1.
so i have this program that i am undertaking.....this project needs to store customer details, edit them and delete them...now i am facing the problem of deriving a code to edit those details.....and if someone has a way out bring me out of this misery...sooo i have pasted my project below for you. thanks in advance
MY CODE
HTML Code:
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int choice [1];
string name;
string idnumber;
string address;
string surname;
string FileName;
fstream myfile;
string line;
cout<<"================================================================================"<<endl;
cout<<" CUSTOMER DETAILS FILE "<<endl;
cout<<"================================================================================"<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<" "<<endl;
cout<<"******************************************************************************** "<<endl;
cout<<" 1) ADD NEW CUSTOMER "<<endl;
cout <<" "<<endl;
cout<<" 2) EDIT CUSTOMER'S DETAILS "<<endl;
cout<<" "<<endl;
cout<<" 3) DELETE CUSTOMER'S DETAIL "<<endl;
cout <<" "<<endl;
cout <<" 4) VIEW CUSTOMER DETAILS "<<endl;
cout <<" "<<endl;
cout <<" 5) BACK TO MAIN PAGE "<<endl;
cout<<"******************************************************************************** "<<endl;
cout<<" "<<endl;
cout<<" please enter your choice :";
cin>> choice[1];
system("cls");
if (choice [1]==1)
//asking the user to input required data
{
cout <<" "<<endl;
cout <<"================================================================================"<<endl;
cout <<" NEW CUSTOMER "<<endl;
cout <<"================================================================================"<<endl;
cout <<" "<<endl;
cout <<" "<<endl;
cout <<" "<<endl;
cout <<"enter customer name :" ;
cin >> name;
cout <<" "<<endl;
cout <<"enter customer surname :";
cin>>surname;
cout <<" "<<endl;
cout <<"enter customer I.D :";
cin>>idnumber;
cout <<" "<<endl;
cout <<"enter customer aaddress :";
cin>>address;
// saving inputted text
ofstream myfile;
myfile.open ("TBS.txt",ios::out|ios::app);
myfile << name<<'\t'<<surname<<'\t'<<idnumber<<'\t'<<address<<endl;
myfile.close();
system("cls");
cout <<""<<endl;
cout <<"================================================================================ "<<endl;
cout <<""<<endl;
cout <<"*************NEW CUSTOMER DETAILS HAS BEEN ADDED SUCCESFULLY***** "<<endl;
cout <<""<<endl;
cout <<"================================================================================ "<<endl;
}
else if (choice [1]==2)
{
cout <<" "<<endl;
cout <<"================================================================================"<<endl;
cout <<" EDIT CUSTOMER DETAILS"<<endl;
cout <<"================================================================================"<<endl;
cout <<" "<<endl;
cout <<" "<<endl;
}
else if (choice[1]== 4)
{
string getcontent;
ifstream myfile ("TBS.txt",ios::in);
if(myfile.is_open())
{
while( myfile.eof())
{
myfile >> getcontent;
cout << getcontent <<endl;
}
}
}
system("PAUSE>null");
return 0;
}
Re: editing saved records in a text file
Welcome to codeguru. You did a nice job using tags for your very first post but code tags [code] Code here [/code] are more appropriate since they preserve the indentation.
The declaration creates an array of 1 integers which you really don't need. is enough. Also, arrays in C/C++ are indexed from 0 and up so where you use choice [1] your index is out of bounds.
Re: editing saved records in a text file
thanks just removed that .............but could u pliz help me by derivinga code to edit those deals
Re: editing saved records in a text file
How did you want to search the customer and edit its record. By its name, id or something else? If you can store each customer information in one line then it might be easy to read text file line by line and parse each line and retrieve information.
NOTE:To modify anything in text file, you will need to read the entire file, row by row, saving it in memory with appropriate modifications, and rewrite the entire file.
You can also write a parser and load complete file at once and extract each customer information and store in a map, Manipulate your records in memory and save everything at once in a file.
Suggestion . Use XML if you really want to store information in text based files.
Re: editing saved records in a text file
May I ask why you are using a flat text file? There are much better ways to do that, XML or JSON as Naumaan said, but for something like this, I would highly recommend using a database.