Thread: Appending Random Text File.

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    12

    Appending Random Text File.

    having Trouble with this..

    I want to overwrite the Name inside the Text file with the Name i input but what is happening it is only adding the Name to the End of the Text File. And its still Reading in the Old Name not the new one

    Code:
    include <iostream>
    #include<sstream> 
    #include<fstream> 
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    struct Newpassenger
    { 
        int seat_number;
        char name[20];
        char onward[3];
    };
    
    int main() 
    {
        int i;
        
    
     
            if (choice == 2 ) // Update Record.. 
                
            {
                {
                    fstream Airline ("AirLine.text", ios::out | ios::in | ios::app | ios::binary); 
                    if(!Airline)
                    {
                        cout << "File Could not be opened." << endl;  
                        system("PAUSE");
                        exit (1); 
                    }
                    
                    
                    cout << "Enter Seat Number to Update"
                    <<"(1 to 100, 0 to end )?";
                    
                    
                    Newpassenger passenger;
                    cin >> passenger.seat_number;
                    
                    
                    while (passenger.seat_number > 0 && passenger.seat_number <=100)
                    {
                        
                        Airline.seekg((passenger.seat_number - 1 ) * sizeof(Newpassenger),ios::beg);
                        Airline.read(reinterpret_cast<char *>(&passenger), sizeof(Newpassenger));
                        
                        
                        
                        
                        cout << " Found Record. Now Enter New Name:";
                        cin >> passenger.name;
                        
                        Airline.seekp((passenger.seat_number - 1 ) * sizeof(Newpassenger), ios::beg);
                        Airline.write(reinterpret_cast<const char *>(&passenger), sizeof (Newpassenger));
                        
                        cout << "\nEnter Seat Number: ?";
                        cin >> passenger.seat_number;
                        
                        
                    }
                    
                    
                    
                    Airline.close();
                    cout << endl;
                }         
                
                
                
                
            }
    
    
    
            if (choice == 4 ) //Read a Seat Number..
            {
                
                
                fstream Airline ("AirLine.text",ios::in |ios::binary); 
                if(!Airline)
                {
                    cout << "File Could not be opened." << endl;  
                    system("PAUSE");
                    exit (1); 
                }
                
                
                cout << "Enter Seat number to Print "
                << "(1 to 100, 0 to end)? ";
                
                Newpassenger passenger;
                cin >> passenger.seat_number;
                
                while (passenger.seat_number > 0 && passenger.seat_number <=100)
                {
                    Airline.seekg((passenger.seat_number - 1) * sizeof(Newpassenger));
                    
                    Airline.read(reinterpret_cast<char *>(&passenger), sizeof(Newpassenger));
                    
                    if (passenger.seat_number !=0)
                        cout << passenger.name << " \n "
                        << passenger.onward << " ";
                    
                    else 
                        cout << " No record for that Seat Number.\n";
                    
                    cout << "\nEnter Seat Number: ?";
                    cin >> passenger.seat_number;
                }
                cout << endl;
                Airline.close();
                
            }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Why don't you just implement it as a .csv file? or tab seperated values? It would be much easier to navigate the file that way. Or if your file already incluides a delimiter, use that as a field reference.
    Depending on the size (or potential size..) of the input file you could just buffer all the data in an appropriate container of strings, perhaps a map, or map of your datatype. then just parse and replace as required.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > what is happening it is only adding the Name to the End of the Text File.

    Perhaps read what ios::app really does.
    Input/Output with files - C++ Documentation

    Also, you write the file, THEN read the seat number (huh?)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Im not Writing to the File before Reading,
    Yes all that is happening it is Writing to the End of the Location in the File,

    Im asking a User to Input Seat Number... they Enter 3

    it seeks to that location once it is found it is then Asking a User to enter a New Name
    Code:
     Airline.read(reinterpret_cast<char *>(&passenger), sizeof(Newpassenger));
                        
                        
                        
                        
                        cout << " Found Record. Now Enter New Name:";
                        cin >> passenger.name;
    Last edited by jackirl; 11-27-2011 at 10:02 AM.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You are over complicating a very simple thing, or is it that you have to do it like this for an assignment? Does your data exist prior to the program being run? If not then you will have to write it... ! If you append to a file it will be placed at the end of the current data, as already pointed out to you. If you insist on doing this in the manner you are attemting then how is the correct file position known? Is it flagged with a digit? like 3? I just cant see why you would want to do it like this, just table your data , parse, update, save .
    Last edited by rogster001; 11-27-2011 at 10:37 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Yes it is for a project.

    i have 100 blank records being Set up inside the Array then i a menu
    insert
    update
    remove

    insert / remove are all working it is just the update is giving me a headache

    when i do an update Record needs to be filled for that Seat Number before im allowed to edit.

    it is flagged with the Seat Number.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Got it Working, just change App to ate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Access Text file
    By Dkamaque in forum C Programming
    Replies: 2
    Last Post: 03-11-2011, 09:29 AM
  2. appending text to existing text file
    By kpax in forum C Programming
    Replies: 8
    Last Post: 05-28-2008, 08:46 AM
  3. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  4. Appending to a text file
    By mike_g in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2007, 10:35 AM
  5. appending to/reading from text file
    By BR7 in forum C Programming
    Replies: 5
    Last Post: 02-22-2002, 05:02 PM