Writing and modifying data in a file

This is a discussion on Writing and modifying data in a file within the C Programming forums, part of the General Programming Boards category; Hi, I decided to write simple database program. It is a database in which student's data are represented like this ...

  1. #1
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712

    Writing and modifying data in a file

    Hi,
    I decided to write simple database program.
    It is a database in which student's data are represented like this
    Code:
    typedef struct student
    {
    	char first_name[31];
    	char last_name[31];
    	unsigned long stud_id;
    	struct student* next;
    }student;
    Because program should allow user to enter new records, change records and list records I decided to use linked list because it is dinamical structure.
    All iformations should be saved in appropriate file.
    Writing to a file is something like this:
    Code:
    while(head != NULL)
    {
    	fwrite(head,sizeof(student),1,fp)/*fp is FILE pointer*/
    	head = head->next;
    }
    And that is OK.
    Now assume there are already records saved in the file. When user starts program, data file is opened and data must be loaded. Now I need to decide what approach to use. One idea is to form a new linked list from data in the file and when saving, file is overwritten and new (and updated) data are written. Obviously for large number of records this is slow and unappropriate.
    Second choice is to work with data in file directly. For example user need to change data of student with some specific ID. First it is necessary to find that record posibly using something like this:
    Code:
    do{
    	fread(stud,sizeof(student),1,fp)/* stud is a pointer to student*/
    }while(stud->sutd_id != ID);
    Then a new updated record needs to be written instead. I think to achive this with following code
    Code:
    	/*change data in stud*/
    	fseek(fp,-(int)sizeof(student),SEEK_CUR);/* go back one record*/
    	fwrite(stud,sizeof(student),1,fp);/* write updated record*/
    With this approach I'll save time but I don't know how clever it is to write and change data directly in file.

    I hope you understand my dillema. I'll need your advices on how to plan this before I start coding. Maybe there is another (better) approach.

    Thank you all for help!

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Now assume there are already records saved in the file. When user starts program, data file is opened and data must be loaded. Now I need to decide what approach to use. One idea is to form a new linked list from data in the file and when saving, file is overwritten and new (and updated) data are written. Obviously for large number of records this is slow and unappropriate.
    why not append to the existing file? that way you dont need to create anything new or overwrite data or files.
    When no one helps you out. Call google();

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Yes, I realize that, because of that I prefer second choice to handle data directly in file. I'm interested if this is done in "the real world". Possible problem is when deleting record.
    For example in file I have records written like this
    <record1><record2><record3><record4>
    Now if I want to delete record2 I assume I'll need to load rest (rec3 and rec4) to form linked list and use fwrite to overwrite rest of the file.
    Any advices would be very valueable to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading command line or text file...expression tree (sort of)
    By mathwork orange in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2008, 08:52 AM
  2. Fade in out of Wav file
    By jase_dukerider in forum C Programming
    Replies: 0
    Last Post: 04-14-2005, 05:06 AM
  3. professional programmers, do you spend more time writing or modifying code
    By Terrance in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 11-25-2002, 09:54 PM

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