writing a struct to file

This is a discussion on writing a struct to file within the C Programming forums, part of the General Programming Boards category; struct employee { char name[30]; char hiredate[10]; float salary; }; employee empl; -------------------------------------------------------------------------------- 1) fwrite(&empl, sizeof(struct employee), 1, fptr); sizeof ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    13

    writing a struct to file

    struct employee
    {
    char name[30];
    char hiredate[10];
    float salary;
    };


    employee empl;

    --------------------------------------------------------------------------------

    1) fwrite(&empl, sizeof(struct employee), 1, fptr); sizeof type

    2) fwrite(&empl, sizeof(empl), 1, fptr); sizeof variable


    Considering that name will be variable length,
    is there going to be a difference which fwrite i use ?

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: writing a struct to file

    Originally posted by strobe9
    Considering that name will be variable length,
    is there going to be a difference which fwrite i use ?
    Name is not variable length. Name is 30 characters long.
    Code:
    printf("sizeof(struct employee) = %d\n", sizeof(struct employee));
    printf("sizeof(empl) = %d\n", sizeof(empl));

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    95
    I thought printing to a file was the same as the screen with regards to strings, it prints everything upter the \0 char, in which case 30 characters (29 to be exact) is the max.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    >1) fwrite(&empl, sizeof(struct employee), 1, fptr); sizeof type
    >2) fwrite(&empl, sizeof(empl), 1, fptr); sizeof variable
    Both are equivalent

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    13
    Thank you everyone.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-05-2009, 05:35 AM
  2. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  3. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  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