file handling problem

This is a discussion on file handling problem within the C++ Programming forums, part of the General Programming Boards category; I have to make a file (or a database I should say)in which I can read,write,append ---I can do these ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    9

    file handling problem

    I have to make a file (or a database I should say)in which I can read,write,append ---I can do these things easily.

    The problem is that how I can rewrite at a particular location.

    Also how I can sort the set of records (if I have some numerical values--weights attached to each record) when those weights keep on changing and the record needs to be sorted each time
    weights are upgraded.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well, as for your first question, look into binary file reading and writing. you'll need to used set data field sizes which can be a little wasteful if used wrong, but it's what you'd need.

    as for you second question, sort the list using a sort algorithm, then when you need to update, pull the record out of the list when the change goes through, just insert the record back into the DB in the right place.

    be warned: there's a HUGE difference between a file and a database... some databases have special commands for this type of thing (look into SQL), whereas with a file you can do the same thing, but there's alot more manual coding involved.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    9
    but how can I "insert" the record in the right place.
    Is there a command for this.
    OR it will involve erasing the whole database and putting the sorted one at that place.
    Is there a way through which I can sort the objects in the file---I mean is there some pointer like thing in the files

    I am using c++ as my programing language
    suggestions even in 'C' will do.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by aditya1
    I mean is there some pointer like thing in the files
    Yes there is. Check out istream::tellg(), istream::seekg() and ostream::tellp(), ostream::seekp().
    Kurt

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    like I said, look into binary file reading/writing. It'll give you exactly what you need. What Zuk just gave you are all parts of binary file I/O.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by aditya1
    but how can I "insert" the record in the right place.
    Is there a command for this.
    OR it will involve erasing the whole database and putting the sorted one at that place.
    Is there a way through which I can sort the objects in the file---I mean is there some pointer like thing in the files

    I am using c++ as my programing language
    suggestions even in 'C' will do.
    Rewriting the whole file every time you want to add a new record is just too time consuming. Databases always append new records at the end of the file, or re-use the space occupied by deleted records. If the file is supposed to be sorted, then the database maintains another much smaller file that contains the key field value and record number. In much smaller applications and files like you might write in school you really don't need the key file but read the data file on program startup and sort the records in memory.

    Records are not really deleted either. When you want to delete a record, just mark it for deletion and reuse the space the next time you want to add a new one. One way to mark it for deletion is to overwrite they key field with some known value -- maybe '~' character or something else that is unlikely to be a valid value. Then during program startup, keep a linked list of these deleted records so that it can be checked when adding a new record.


    So, during program startup, the program would read the data file and build two linked lists -- one list contains key field values and record numbers, the second list is the record numbers of previously deleted records.


    The above will not really work in multi-user environment where two or more people (or programs) need to access the database at the same (or nearly the same) time. If one program adds a record to the file the other program won't know about it. That's why large database programs use a lot more complex algorithms, which are the topics of a whole semester or two (probably even Ph.D. thesis).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 05:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  4. Problem while dealing with file handling functions
    By RoshanGautam in forum C Programming
    Replies: 3
    Last Post: 02-22-2006, 01:42 AM
  5. file handling and function problem.
    By prepare in forum C Programming
    Replies: 7
    Last Post: 10-09-2004, 03:26 AM

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