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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

c++-binary file, delete line(record?)?

  • 12-03-2006 9:26pm
    #1
    Closed Accounts Posts: 1,061 ✭✭✭


    Ok so I have a file in binary format and it contains say(for example):
    1 John 623488 200
    2 Mary 9284774 300
    3 Patrick 224443 120
    4 Eoin 393843 300
    5 Mark 234443 600

    I have a function coded for searching for a particular number or name(both funcitons are very very similiar). I want the user to be able to delete a name and the whole line(all associated data with the name).
    How do I do this?
    I have a struct declared globally(bad/good? not what i'm concerned about at this point, i'll deal with it later if i can)
    Here is something like what I have:
    struct dataStruct
    {
         int number;
         char name[20];
         int phno;
         int amnt;
    }
    

    the code to open the file is:
    file.open("datafile.dat", ios::binary | ios::in);

    and then to read in data:
    dataStruct item;
    file.seekg(0L, ios::beg);
    file.read((char *) &item, sizeof(dataStruct));
    

    So say I want to delete the record "3 Patrick 224443 120"
    How do I go about it?
    I would like to be able to pass the position of that piece of data to another function and delete(or maybe even change) it there.
    Has it got something to do with tellg() / tellp()?
    I looked up tellg and tellp online, but nothing really helped...


Comments

  • Registered Users, Registered Users 2 Posts: 2,082 ✭✭✭Tobias Greeshman


    I want the user to be able to delete a name and the whole line(all associated data with the name).
    How do I do this?
    Ok first of all how are you storing the data you read from the text file? I assume you're going to be using array of structs or a linked list or some other relevant container to store the data.

    To delete a record from an array you need to find the record and move all subsequent records up one position, for a linked list just move the pointer onto the next record and delete the selected record.

    Just delete the appropriate data and then write back to file, by rewriting the file then the deleted record won't exist in the file.

    Having your struct declared the way you have it, is perfectly fine and acceptable.


Advertisement