Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

update File in c\c++

Options
  • 06-01-2003 11:55pm
    #1
    Registered Users Posts: 950 ✭✭✭


    hello Everyone

    i have a problem trying to update a file in a program im writing.

    I can read and write to the file. but i dont know how to update data that has been changed by the user. i have looked up the help and found a seek command but dont know how to use it properly. any info will be a help, either on the seek command or another way of updating the file


Comments

  • Registered Users Posts: 6,240 ✭✭✭hussey


    can you give us some details on your file?

    ie binary data file??
    sequential file? or Random access file?


  • Closed Accounts Posts: 1,325 ✭✭✭b3t4


    not sure if this will help you but
    http://www.cplusplus.com/ref/iostream/istream/tellg.html
    read that site and the one regarding seekg.

    Not too sure that seekg and tellg are exactly what u need.
    tellg gets the current position in the file.
    seekg sets the position in the file to be read from.

    easier way would be to just overwrite the old file with the new version of the file created by the user. (that is if it is a small file, doubt this would be very productive if it was a large file).

    Hope this helps,
    A


  • Registered Users Posts: 950 ✭✭✭jessy


    Thanks for replying hussey.

    It's a sequential file. need to find position of data to be overwritten and then overwrite it with new data, some code to do this will be Appreciated .


  • Registered Users Posts: 6,240 ✭✭✭hussey


    ok what this does is puts 5 simple structs in

    and overwrites struct number 3

    note the casting of struct and the
    ios::ate .. this is like append, but it can write to anywhere in file, append only writes at end

    also seekg this is to read at a specfic position
    seekp write at a specfic position

    compiled fine under vc++ (6)

    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>

    struct Person
    {
    char name[50];
    int age;
    char phone[24];
    };

    int main()
    {
    ofstream outfile;
    ifstream infile;
    outfile.open("test.dat", ios::binary | ios::out);
    Person me;
    int i;

    for (i=0;i<5;i++) {
    Person me = {"Do Ya Like Apples?", i, "123456789"};
    outfile.write((char*)&me, sizeof(me));
    }
    outfile.close();

    infile.open("test.dat", ios::binary | ios::in);

    for (i=0;i<5;i++) {
    infile.seekg(sizeof(me)*i);
    infile.read((char*)&me, sizeof(me));
    cout << me.age << me.name << me.phone << endl;
    }

    infile.close();

    outfile.open("test.dat", ios::binary | ios::ate);

    Person newMe = {"How do ya like these apples", 3, "1800-SAVE-ME"};
    outfile.seekp(sizeof(newMe)*3);
    outfile.write((char*)&newMe,sizeof(newMe));
    outfile.close();

    infile.open("test.dat", ios::binary | ios::in);

    for (i=0;i<5;i++) {
    infile.seekg(sizeof(me)*i);
    infile.read((char*)&me, sizeof(me));
    cout << me.age << me.name << me.phone << endl;
    }

    infile.close();
    return 0;
    }


Advertisement