Need help with Record Update

This is a discussion on Need help with Record Update within the C++ Programming forums, part of the General Programming Boards category; Im trying to Update a Record that is inside a Text file. I wish to do the Following, Search for ...

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

    Need help with Record Update

    Im trying to Update a Record that is inside a Text file.

    I wish to do the Following,
    Search for the Seat No.
    once that is found ask user to enter new Name
    New name will then Replace the Old name inside the text file.

    Code:
     
    		long begin;
                int location = 0;
                
                   fstream myFile("AirLine.text", ios::in | ios::out | ios::app | ios::binary);
                   begin = myFile.tellg();
                   
       cout << "Enter Passenger Seat Number:";
       cin >> key;
              
        
       location = (stArray[i].seat_number -1)* sizeof(passenger); 
       myFile.seekp(location, ios::cur);
        
        
     
       cout << " Enter Passengers New Name:";
       cin >> stArray[i].Newname;
       myFile << stArray[i].Newname;
         
         
        for (i=0; i<N_PASSENGER; i++)
        {
            if (stArray[i].seat_number == key)
                 
                stArray[i].name = stArray[i].Newname;
                myFile << stArray[i].Newname;
        }
        cout << "Passengers name is now " << stArray[i].Newname;
         
     
            }
              
        myFile.close();
    Code:
    struct passenger
    
    { 
        int seat_number;
        string name;
        string Newname;
        string connecting_flight;
    };
    
    int N_PASSENGER;
    struct passenger stArray[30];
    Last edited by jackirl; 11-26-2011 at 07:36 AM.

  2. #2
    Registered User modwind's Avatar
    Join Date
    Dec 2010
    Posts
    15
    jackirl, sizeof(string) has no useful meaning. It does not show real amount of memory required to store a string in a file. And this amount is variable, equal to current number of characters in it.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Im now after moving to Char but still no joy

    Really out of Ideas.. to fix this.

    Code:
    using namespace std;
    
    struct passenger
    
    { 
        int seat_number;
        char name[30];
        char Newname[30];
        char onward[3];
    };
    
    int N_PASSENGER;
    struct passenger stArray[30];
    Code:
    if (choice == 2) // Updates Record..
                
                
                
            {
                long len;
                int recNumb;
               
                
                fstream myFile("AirLine.text", ios::in | ios::out | ios::app | ios::binary);
                len = (stArray[i].seat_number -1)* sizeof(passenger);            
                cout << "Enter Passenger Seat Number:";
                cin >> recNumb;
                
                myFile.seekg(0, ios::beg);
                myFile.read(reinterpret_cast<char*>(&stArray),len);
                
                while (!(myFile.eof()))
               {  
                   if(recNumb == stArray[i].seat_number)
                   {    
                
                        cout << " Enter Passengers New Name:";
                        cin.getline(stArray[i].Newname,30);
                
                     
                
                   }
                
                
                       
                
                
                
                
                      myFile.tellg();
                   // int offset = passenger->len; //one record less in size
                       
                       myFile.seekp(len, ios::beg); 
                       
                       myFile.write (reinterpret_cast <const char*> (& N_PASSENGER), sizeof (passenger));
                       
                     
                       
                       }
                       
                       myFile.read( reinterpret_cast<char *>( &N_PASSENGER), sizeof(passenger));
                       }
                       
                       else 
                       
                       cout<<"Passenger  not found"<<endl;
                       
                       myFile.close();
                       }
        
        
        
            
            
            
        
            
            
            
            /*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/  
            
            
        }
        
        
        
        
        return 0;
    }

  4. #4
    Registered User modwind's Avatar
    Join Date
    Dec 2010
    Posts
    15
    Your code is too complicated. Try something like this (read one record at a time):
    Code:
    	fstream file("AirLine.txt", ios::in | ios::out | ios::app | ios::binary);
    	file.seekg(0, ios::beg);
    
    	passenger p;
    	while (!file.eof())
    	{
    		file.read((char*)&p,sizeof(p));
    
    		/* Check record p here */
    
    		// move 1 record back
    		long pos=file.tellg();
    		pos-=sizeof(passenger);
    		file.seekp(pos,ios::beg);
    
    		/* Change record p here */
    
    		file.write((char*)&p,sizeof(p));
    	}

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    12
    Am i on the Right Step..

    im not really good at I/0 Streams.
    Code:
    if (choice == 2) // Updates Record..
                    
                    
                    
                {
                    int recNumb;
                int location =0;
                    
                    
                    fstream myFile("AirLine.text", ios::in | ios::out | ios::app | ios::binary);
                    myFile.seekg(0, ios::beg);
                    
                    cout << "Enter Passenger Seat Number:";
                    cin >> recNumb;
                    
                    passenger p;
                    
                    while (!myFile.eof())
                    {
                        myFile.read((char*)&p, sizeof (p));
                        
                        
                        //location = (stArray[i].seat_number -1)* sizeof(passenger);
                       // myFile.seekp(location);
                                             
                        
                        long pos = myFile.tellg();
                        pos -= sizeof(passenger);
                        myFile.seekp(pos,ios::beg);
                        
                        if(recNumb == stArray[i].seat_number)
                            
                        {
                            cout << " Enter Passengers New Name:";
                            cin.getline(stArray[i].Newname,30);
                        }
                        else 
                            cout << "User Not Found ";
                        
    
                        
                       
                        
    
                        
                        
                        myFile.write((char*)&p, sizeof(p));
                        
                        
                        
                        
                        
                    }
                    
                    
                  
                   
                    
                    myFile.close();
                
                    
                    
                 }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Update Record & Delete Record in File.
    By unsafe_pilot1 in forum C Programming
    Replies: 13
    Last Post: 05-18-2008, 08:22 AM
  2. New record.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 08-08-2004, 06:26 AM
  3. Record From TV
    By caroundw5h in forum Tech Board
    Replies: 4
    Last Post: 12-17-2003, 09:46 AM
  4. how can i update a record????
    By mattyans in forum C Programming
    Replies: 3
    Last Post: 05-06-2002, 03:22 PM
  5. Get to First Record
    By Colin in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2002, 03:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21