CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2012
    Posts
    2

    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;
    }

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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
    Code:
    int choice [1];
    creates an array of 1 integers which you really don't need.
    Code:
    int choice;
    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    May 2012
    Posts
    2

    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

  4. #4
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    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.
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  5. #5
    Join Date
    Jan 2009
    Posts
    1,689

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured