Thread: a simple question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    a simple question

    fwrite(buffer,size, count,fp);
    fwrite(na, 1, 36, fp)
    na is buffer, fp is fp
    I want to know what do size and count mean here.. thx!!~

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    26
    Code:
    char array[50];
    
    fwrite(array, sizeof(array), 1, file);
    size is the size of the data item you want to write, count is the number of items of that size you want to write.
    The above writes the whole array to the file.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Size would be the size of the record, most commonly you will be using a structure so you would say sizeof(struct Label_Name). And count would be the number of records (structs) you want to write. If you want to write the next 5 recrods (structs) from your array of structures substitute 5 for count.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    int array[100];
    size_t elem_size = sizeof( int ); // size of each element
    size_t elem_count= 100; // the number of elements
    size_t result;
    result = fwrite( array, elem_size, elem_count, fp );
    if ( result != elem_count ) {
        // panic
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    THX!
    But I still have questions.
    The size is 1 in my example.
    so it's very small,quite strange?
    what's the unit of size?
    and what's the meaning of sizeof(array)?
    is that mean the size of the array?
    and your count is 1.Is that mean one value will be wrote?
    THX!!~

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The size is 1 in my example.
    presumably because you have
    char na[36];

    > what's the unit of size?
    a char is the smallest addressable memory unit, and it's size is always 1
    Everything else is an integral multiple of this

    > and what's the meaning of sizeof(array)?
    The size of the whole array

    > and your count is 1.Is that mean one value will be wrote?
    fwrite(array, sizeof(array), 1, file);

    Remember fwrite(buffer,size, count,fp);
    In all these cases, the total number of bytes written to the file is given by size*count
    So, these are the same (usually)
    int array[100];
    fwrite(array, sizeof(array), 1, file);
    fwrite(array, sizeof(array[0]), 100, file);
    fwrite(array, sizeof(int), 100, file);
    fwrite(array, sizeof(array[0]), sizeof(array)/sizeof(array[0]), file);

    The only minor difference between them is the return result - fwrite should return the value of count, if all is well, so in one case it would return 1, but in the others, it would return 100.

    The major difference between them is what happens if there is an error writing the file. In the first case, the count is 1, so either the whole array is written or it isn't.
    In the other cases, the count is 100, so any number of elements of the array (from 0 to 100) could be written - the number written being the return result. Perhaps you could consider saving some of the data to be preferable to saving none of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM