Writing a struct to a binary file problem

This is a discussion on Writing a struct to a binary file problem within the C Programming forums, part of the General Programming Boards category; I have searched the messageboard for a while but didn't find anything similar. I have written the following c prog: ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    Writing a struct to a binary file problem

    I have searched the messageboard for a while but didn't find anything similar.
    I have written the following c prog:

    Code:
    #include <stdio.h>
    
    #define RECSIZE 8+21+13+21+2*sizeof(int)+sizeof(char)
    
    typedef struct {
        char id[8];
        char surname[21];
        char name[13];
        char street[21];
        int number;
        int postcode;
        char condition;
    } pelatis;
    
    int main() {
    
    	FILE *fp1=fopen("dataset1.in","r");
    	pelatis p,p1;
    	fscanf(fp1,"%s %s %s %s %d %d %c",p.id,p.surname,p.name,p.street,&p.number,&p.postcode,&p.condition);
    	
    	FILE *fp2=fopen("testfile","wb+");
    	fwrite(&p,RECSIZE,1,fp2);fseek(fp2,0,SEEK_SET);
    	fread(&p1,RECSIZE,1,fp2);
    	printf("%s %s %s %s %d %d %c\n",p1.id,p1.surname,p1.name,p1.street,p1.number,p1.postcode,p1.condition);
    	fclose(fp1);
    	fclose(fp2);
    }
    The dataset1.in is a text file with records such as:

    A250000 OF PMJXVTEWS HACJDEZ 36 50111 0
    A249999 JN PPERLJB UUNHWSYY 43 58540 0
    A249998 WFUVZZ KGHVAJC BAXHHPRVS 62 53643 1
    A249997 LYTCPKTPO DFLUN TIERMBSNDYDX 91 72358 0
    A249996 ZRD VPEEVMENT HORTSM 111 21071 1

    What I do is to read the first record,write it in the binary file testfile, and then read it and print it to validate the content I previously wrote.

    All the fileds are printed as they should except for the condition field which is a single character.What is printed is:
    Code:
    A250000 OF PMJXVTEWS HACJDEZ 36 50111
    If I change it to int and change the %c to %d,the condition field is printed correctly.

    What am I doing wrong?

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > What am I doing wrong?
    You're assuming that the size of a structure is the same as the sum of the sizes of the members

    RECSIZE != sizeof(pelatis)
    in most environments

    So just use the sizeof the struct, and forget your #define
    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. Replies: 22
    Last Post: 12-23-2008, 12:53 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  5. Replies: 6
    Last Post: 05-12-2005, 03:39 AM

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