Initializing a file

This is a discussion on Initializing a file within the C Programming forums, part of the General Programming Boards category; Hi friends, I am new to file handling and i need some help. How can i initialize a file with ...

  1. #1
    Registered User j.sreejit's Avatar
    Join Date
    Aug 2006
    Posts
    10

    Initializing a file

    Hi friends,

    I am new to file handling and i need some help.

    How can i initialize a file with 100 empty records and then add new records as the user chooses to do so?

    This is what I want to do:-

    Code:
    struct employee
    {
    
    char emp_dept[20]; char emp_name[20]; float salary;
    };
    filename: pers_data.dat

    To begin with the file will contain 100 records, each will look like the foll:
    emp_dept = "unassgined"
    emp_name = ""
    salary = 0.00

    so first 5 records will be

    Code:
    unassgined            0.00
    unassgined            0.00
    unassgined            0.00
    unassgined            0.00
    unassgined            0.00
    Is it possible to update and delete records without creating a new file?


    Kindly help.

    Thanks

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    fread(), fwrite(), fseek(), fopen() -- Check out the man pages on these, for a start.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    First you open the file in binary mode using say
    fp = fopen( "pers_data.dat", "rb" ); // for reading
    fp = fopen( "pers_data.dat", "wb" ); // for writing
    fp = fopen( "pers_data.dat", "r+b" ); // for reading and writing

    If you have a variable say
    struct employee me;

    Reading and writing a record
    if ( fread( &me, sizeof me, 1, fp ) == 1 ) // success
    if ( fwrite( &me, sizeof me, 1, fp ) == 1 ) // success


    Normally, when you read and write a record, the file automatically advances to the next record. So for example you can read the whole file in a simple loop.

    To jump to a specific record (first record is record 0),
    fseek( fp, recNum * sizeof me, SEEK_SET );

    > Is it possible to update and delete records without creating a new file?
    Yes, just have a field in your structure which means 'deleted' and then write that to the file.
    Update is just an fseek followed by an fwrite

    One final point, if you're doing lots of reading and writing, it's a good idea to do
    fflush( fp );
    after every fwrite.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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