Thread: Update Records in c++ using Sequential Files

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    Update Records in c++ using Sequential Files

    How do i go about updating a specific record in sequential File using a primary key in c++.
    I used Inventory number as my primary key..please any assistance?
    Code:
    int locate[2];
        string fname, lname, add, name, address;
        int num, foundit;
        Customer customer;
        long len;
    
    
        ifstream file("Clinic Files.txt", ios::in);
        //len = sizeof(Customer);
    
    
        cout << "Enter Inventory number for customer you would like to Update" << endl;
        cin >> locate[0];
    
    
        file.seekg(0, ios::beg); //0 bytes from beginning of file(ios::beg) 
    
    
        //CHECKS IF USER EXITS IN SYSTEM
        while(file >> locate[1] >> fname >> lname >> num >> address){
    
    
            if (locate[0] == locate[1]){
                foundit = 1;
                cout << "Success User was found " << endl;
    
    
                cout << "Select the data field you want to change" << endl;
                int findIt = 0;
    
    
                cout << "1-Firstname" << endl;
                cout << "2-LastName" << endl;
                cout << "3-Phone Number" << endl;
                cout << "4-Address" << endl;
                cin >> findIt;
    
    
                string No, Fname, Lname, Add, Phone;
                // int Phone;
                fstream file("Clinic Files.txt", ios::in | ios::out);
    
    
                file >> No >> Fname >> Lname >> Phone >> Add;
    
    
                if (findIt == 1){
                    int n;
                    string a;
                    cout << "Enter The Updated Firstname" << endl;
                    cin >> a;
                        n = a.size();
                        cout << n;
                        file.seekg(0, ios::beg); //move the put cursor to start of "text"
    
    
                    for (int i = 0; i<n; i++){
                        file << a;
    
    
                    }
                    file.close();
                    break;
                    
                }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If it's a text file with variable length records, your only choice is to write a new file with all your modifications, and copying of data you want to keep.
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've got the same line of code:
    Code:
    file.seekg(0, ios::beg)
    twice with two different comments next to it -- it can't both set the file pointer to the beginning of the file and later set the file pointer to the beginning of the record.

    Also, I don't believe that you can count on your second file variable having the same file pointer as the first file variable, even if they are pointing to the same file, so the second file will always be examining the first record in the file.

    You don't seem to be making any attempt to make the record format fixed-width; that means any changes that make a field a different size will require rewriting the entirety of the file from that point forward (if you want to edit in-place, so to speak).

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Quote Originally Posted by tabstop View Post
    You've got the same line of code:
    Code:
    file.seekg(0, ios::beg)
    twice with two different comments next to it -- it can't both set the file pointer to the beginning of the file and later set the file pointer to the beginning of the record.

    Also, I don't believe that you can count on your second file variable having the same file pointer as the first file variable, even if they are pointing to the same file, so the second file will always be examining the first record in the file.

    You don't seem to be making any attempt to make the record format fixed-width; that means any changes that make a field a different size will require rewriting the entirety of the file from that point forward (if you want to edit in-place, so to speak).
    ok can you assist me with a sample code to show me how it can be done or modifying my code to make it work??
    Last edited by shavar; 11-03-2013 at 12:35 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't know what specifications you need to follow, so I could only give general guidelines. Also, I suspect that you would have to rewrite about 80% of your above code, and I'm not going to write 80% of your assignment.

    If you couldn't (or don't want to) do fixed-width records, then you would want to read in the entire file and store it in memory before you start doing any modifications; you can then freely modify your own internal copy and then, when the time comes, write the whole thing back to the file.

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    how would i go about reading specific line of text from sequential file into memory? thinking about using a char buffer[90];

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This is C++. You should be using strings. (Granted if you do use a fixed-width record, you will know what the length is ahead of time, but even so.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Update and search records in files
    By Zhafri As-said in forum C Programming
    Replies: 4
    Last Post: 05-08-2012, 04:22 PM
  2. sequential access files?
    By swansea in forum C++ Programming
    Replies: 19
    Last Post: 04-29-2010, 05:46 PM
  3. Sequential and binary files in c
    By CiaranB in forum C Programming
    Replies: 2
    Last Post: 05-08-2008, 09:26 AM
  4. Sequential Records
    By TeamGreen in forum C++ Programming
    Replies: 3
    Last Post: 01-06-2004, 05:55 PM
  5. Files&Records(Entering records)
    By Beginnerinc in forum C Programming
    Replies: 1
    Last Post: 01-29-2003, 09:11 AM