Welcome, Guest. Please login or register.

Author Topic: fwrite() count?  (Read 9267 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline iamaboringpersonTopic starter

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 5744
    • Show only replies by iamaboringperson
fwrite() count?
« on: January 24, 2003, 02:38:35 AM »
can anyone tell me in (very very very very) great detail, what the count argument on the ansi stdio.h fwrite() function does?

ive tried using it but only got crap results!
 

Offline Xand

  • Sr. Member
  • ****
  • Join Date: Feb 2002
  • Posts: 391
    • Show only replies by Xand
Re: fwrite() count?
« Reply #1 on: January 24, 2003, 03:02:19 AM »
Check your PM.
SunBlade 1000 2x1200mhz UltraSparc III+, 8GB RAM, XVR1000, 73GB and 300GB FC drives, DVD, SunPCi IIIpro - 1GB RAM (yes it does work when I set the clock back to 2009), Solaris 10.

Ultra60 2x450MHz UltraSPARC II, 2GB RAM, 9GB U160 and 36GB U320 drives, CDROM
 
A1200T, 603e-240mhz/040-50mhz SCSI, 256mb, MediatorSX, Voodoo3-3000, Soundblaster 128, 10/100 Ethernet, other stuff OS3.9
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16867
  • Country: gb
  • Thanked: 4 times
    • Show only replies by Karlos
Re: fwrite() count?
« Reply #2 on: January 24, 2003, 11:39:34 AM »
It's supposed to be the number of elements of a given size to write. The prototype is something like

size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream)

so if you have an array of 32-bit floats youre planning to write to a file you'd use something like this

/* create an array on the free store */
size_t arraySize = 100;
float *myArray = (float*) malloc(arraySize*sizeof(float));

/*
...Assuming malloc() was ok and so on...
...do some interesting stuff with our array
*/

/* lets assume the file is already opened as outFile */

elementsWritten = fwrite(myArray, sizeof(float), arraySize, outFile);

Assuming all was OK, elementsWritten should equal arraySize. Anything less was caused by an IO write failure of some description, in which case elementsWritten was (as the name implies) the number of elements successfully written before the IO error occured.

Thats about it, really...

int p; // A