gtpc2m3iC/C++ Language Support User's Guide

fwrite-Write Items

This function writes items.

Format

#include <stdio.h>
size_t fwrite(const void *buffer, size_t size, size_t count, FILE *stream);

buffer
The address of the data to be written.

size
The size of each item to be written.

count
The number of items to be written.

stream
The stream to be written.

This function writes up to count items of size size from the location pointed to by buffer to the stream pointed to by stream.

Because the fwrite function may buffer output before writing it out to the stream, data from previous fwrite calls may be lost whereby a subsequent call to the fwrite function causes a failure when the buffer is written to the stream.

The fwrite function has the same restriction as any write operation for a read immediately following a write, or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must be an intervening reposition unless an end-of-file (EOF) has been reached.

Normal Return

If successful, the fwrite function returns the number of items that were written. If there is no error, the return value is the same as count.

Error Return

The return value can be smaller than count only if a write error occurs.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Examples

The following example writes NUM long integers to a stream in binary format. It checks that the fopen function is successful and that 100 items are written to the stream.

#include <stdio.h>
#define  NUM  100
 
int main(void)
{
   FILE *stream;
   long list[NUM];
   int numwritten, number;
 
   if((stream = fopen("myfile.dat", "w+b")) != NULL )
   {
     for (number = 0; number < NUM; ++number)
       list[number] = number;
     numwritten = fwrite(list, sizeof(long), NUM, stream);
     printf("number of long characters written is %d\n",numwritten);
   }
   else
     printf("fopen error\n");
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.