fwrite array of structs

This is a discussion on fwrite array of structs within the C Programming forums, part of the General Programming Boards category; Why doesn't fwrite write the whole array? Also, can fwrite write any kind of data not just char? Code: #include ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    fwrite array of structs

    Why doesn't fwrite write the whole array? Also, can fwrite write any kind of data not just char?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    struct list{
    	int num;
    	char name[80];
    };
    
    void save(struct list names[2]);
    main()
    {
      struct list names[2] = {{1,"Name"},{2,"Name"}};
      save(names);
      getch();
      return 0;
    }
    
    void save(struct list names[2])
    {
      FILE *fp;
      fp = fopen("names.dat","wb");
      fwrite(names,sizeof(names),2,fp);
      fclose(fp);
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Check the sizeof operator using the printf function...
    sizeof(names) calculates the size of the address and not the size of the structure.

    Try sizeof(struct list) or sizeof(*names).

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    My books shows that fwrite(names,sizeof(names),1,fp); should write entire array to disk. I changed sizeof to struct list and still get the same results. Also my books says
    fwrite(names,sizeof(names),1,fp); is the same as
    fwrite(names,sizeof(names[0]),2,fp); and neither does what I want them to do.

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    I think that's because the name variable in the book is not an argument.
    When you print the size of name in main you see the size is 168 bytes.
    When you pass it as an argument, you pass the address...
    Code:
    void save(struct list names[2])
    {
      printf("sizeof(names) = %d\n", sizeof(names)); /* 4 because it's an address */
      printf("sizeof(names) = %d\n", sizeof(names[0])); /* 84 because it's the size of the first structure */
    }
    
    main()
    {
      struct list names[2] = {{1,"Name"},{2,"Name"}};
      printf("sizeof(names) = %d\n", sizeof(names)); /* 168 */
      save(names);
      getch();
      return 0;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > My books shows that fwrite(names,sizeof(names),1,fp); should write entire array to disk
    All of what you've read is only true if the fwrite call is in the same function as the array itself.

    When you pass an array to a function (like save), all the size information of the array disappears, and all sizeof will tell you is the size of the pointer (to the array)

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Ok, I understand most of it now. Does the third argument for fwrite have to be the number of array elements I'm writing to disk? Again, can fwrite write any kind of data like structs containing integers?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > Does the third argument for fwrite have to be the number of array elements I'm writing to disk
    No, but it usually is

    Eg
    int arr[10];

    You can write the whole lot like this
    size_t n = fwrite( arr, sizeof(arr[0]), 10, fp );

    or like this
    size_t n = fwrite( arr, sizeof(arr), 1, fp );

    The only difference is in the result which is returned - which should be equal to the 3rd parameter if all is well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  2. Replies: 41
    Last Post: 07-04-2004, 03:23 PM
  3. Creating a pointer to an array of structs
    By Jonathan_ingram in forum C Programming
    Replies: 8
    Last Post: 12-31-2003, 07:49 AM
  4. array of structs initialization - PLZ help...
    By Vanya in forum C++ Programming
    Replies: 2
    Last Post: 12-11-2002, 07:10 PM
  5. Pointer to Array of Structs
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 07:34 AM

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