I/O operation on binary file

This is a discussion on I/O operation on binary file within the C Programming forums, part of the General Programming Boards category; Dear all: I have the following: Code: typedef struct { int number; char str[15]; } RECORD; #define NUM_RECORDS (50) int ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    37

    I/O operation on binary file

    Dear all:

    I have the following:

    Code:
    typedef struct {
            int number;
            char str[15];
    } RECORD;
    
    #define NUM_RECORDS (50)
    
    int main () {
            RECORD records;
            int i;
            
            FILE *fptr;
    
            fptr = fopen ("records.dat", "w+");
            
            for (i=0; i<NUM_RECORDS; i++) {
                    records.number = i;
                    sprintf(records.str, "RECORD - %d", i);
                    fwrite (&records, sizeof(&records), 1, fptr);
            }
    
            fclose (fp);
    
            fptr = fopen ("records.dat", "r+");
            fseek (fptr, 30*sizeof(records), SEEK_SET);
            fread (&records, sizeof(records), 1, fptr);
    
            records.number = 500;
            sprintf(records.str, "RECORD - %d", records.number);
    
            fseek (fptr, 30*sizeof(records), SEEK_SET);
            fwrite (&records, sizeof(records), 1, fptr);
    
            fseek (fptr, sizeof(records), SEEK_END);
            rewind (fptr);
            for (i=0; i<NUM_RECORDS; i++) {
                    fread(&records, sizeof(records), 1, fptr);
                    printf("%d\n", records.number);
                    printf("%s\n", records.str);
            }
    
            fclose (fptr);
            return 0;
    }
    But it didn't print all 50 records in this format:

    0
    RECORD - 0
    1
    RECORD - 1
    .
    .
    .
    .

    Instead, it prints the following:

    0
    
    4

    8

    12

    16
    
    20
    
    24
    
    28
    
    32
    !
    36
    %
    40
    )
    44
    -
    48
    1
    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    0

    500
    RECORD - 500
    500
    RECORD - 500
    500
    RECORD - 500
    500
    RECORD - 500
    500
    RECORD - 500
    500
    RECORD - 500
    500
    RECORD - 500

    What did I do wrong?

    Thanks.

    barramundi9

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Look at your sizeof expression on line 19

    Before you do that, load your records.dat into a hex editor, and see what you actually wrote.
    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.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    37
    OMG ... I am so dumb ...

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help me in Binary XOR operation using C++?
    By maxstoto in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2011, 02:24 PM
  2. File operation: Clarification for fwrite.
    By sureshreddy.ap in forum C Programming
    Replies: 2
    Last Post: 01-04-2011, 04:54 AM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. performing a search-and-replace operation on a file
    By dakolmon in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2004, 10:58 AM
  5. once mor file operation
    By stormbringer in forum C Programming
    Replies: 4
    Last Post: 12-20-2002, 06:31 AM

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