how to delete or modify a record in a file?

This is a discussion on how to delete or modify a record in a file? within the C++ Programming forums, part of the General Programming Boards category; I create a file like this: Code: ... class Student { ... }; ifstream fin ( fname ); fin.write ( ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    1

    how to delete or modify a record in a file?

    I create a file like this:
    Code:
    ...
    class Student { ... };
    ifstream fin ( fname );
    fin.write ( (char*)stud, sizeof ( Student );   //save student records
    ...
    ofstream  fout ( fname );
    ...
    i found i can only read the records , or position the file pointer to
    a specific record, however, i do not know how to delete a record
    from a file, or modify of course .

    thanks a lot for help !

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    If you know the position of the file you want to alter in the file then you can overwrite it, and you can append a new record to a file. But beyond that, it's probably best to read the file into your program, make the alteration, and write it back again. If you need to delete an entire record you will need to use some mechanism to indicate you should ignore the material at a given location in the file, or, again, write the entire file to a container in the program, delete the record from the container, shift information within container, if necessary, and then write the entire container back to file.

  3. #3
    twm
    twm is offline
    root
    Join Date
    Sep 2003
    Posts
    232
    >i do not know how to delete a record from a file, or modify of course
    Incentive to keep the entire file in memory. Otherwise you end up using swap files, and that's ugly (or slow...usually both). If your file doesn't fit in memory all at once, your files are too big, you should consider a ban on excessive use of ios::app.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Deleting / Changing a record in a text file
    By clearrtc in forum C Programming
    Replies: 9
    Last Post: 08-21-2006, 12:09 AM

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