Write elements to a file
Synopsis:
#include <stdio.h>
size_t fwrite( const void* buf,
               size_t size,
               size_t num,
               FILE* fp );
 
Arguments:
- buf
 
- A pointer to a buffer that contains the elements that you want to write.
 
- size
 
- The size of each element to write.
 
- num
 
- The number of elements to write.
 
- fp
 
- The stream to which to write the elements.
 
 
Library:
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
 
Description:
The fwrite() function writes num elements of
size bytes each to the stream specified by fp.
Note: 
If you're reading and writing large amounts of data, you can improve performance by increasing the size
of the internal buffer that's used for stream I/O.
For more information, see
Adjusting the buffer size
in the entry for 
fopen().
 
 
Returns:
The number of complete elements successfully written. If an error occurs,
this is less than num, and errno is set to indicate the type of error.
 
Errors:
    
        
            - EAGAIN
 
            - The O_NONBLOCK flag is set for the file descriptor
                underlying fp, and the process would be delayed in the
                write operation. 
            
 
        
        
        
            - EBADF
 
            - The file descriptor underlying fp isn't a valid
                file descriptor that's open for writing.
            
 
        
        
        
            - EFBIG
 
            - One of the following:
                
                
                    - An attempt was made to write a file that exceeds the maximum file size. 
 
                    
                    
                    
                    - The file is a regular file, and an attempt was made to write at or
                        beyond the offset maximum associated with the corresponding stream.
                    
 
                    
                
             
        
        
        
            - EINTR
 
            - The write operation was terminated due to the receipt of a signal,
                and no data was transferred. 
            
 
        
        
        
            - EIO
 
            - One of the following: 
                            - A physical I/O error occurred.
 
                            - The process is in a background process group attempting to write to
                                its controlling terminal, and either the process is ignoring or
                                blocking the SIGTTIN signal or the process group is
                                orphaned. 
 
                            - (QNX Neutrino extension) The filesystem resides
                                on a removable media device, and the media has been forcibly
                                removed. 
 
                        
 
        
        
        
            - ENXIO
 
            - A request was made of a nonexistent device, or the request was outside
                the capabilities of the device. 
            
 
        
        
        
            - ENOSPC
 
            - There was no free space remaining on the device containing the file.
 
        
        
        
            - EPIPE
 
            - An attempt was made to write to a pipe or FIFO that wasn't open for
                reading by any process.
                A SIGPIPE signal is also sent to the thread. 
            
 
        
        
    
 
Examples:
#include <stdio.h>
#include <stdlib.h>
struct student_data {
    int student_id;
    unsigned char marks[10];
};
int main( void )
{
    FILE *fp;
    struct student_data std;
    int i;
    fp = fopen( "file", "w" );
    if( fp != NULL ) {
        std.student_id = 1001;
        for( i = 0; i < 10; i++ ) {
            std.marks[i] = (unsigned char)(85 + i);
        }
        /* write student record with marks */
        i = fwrite( &std, sizeof( struct student_data ), 1, fp );
        printf( "Successfully wrote %d records\n", i );
        fclose( fp );
        
        if( i == 1 ) {
            return EXIT_SUCCESS;
        }
    }
    return EXIT_FAILURE;
}
 
Classification:
ANSI,
POSIX 1003.1
| Safety: | 
  | 
| Cancellation point | 
Yes | 
| Interrupt handler | 
No | 
| Signal handler | 
No | 
| Thread | 
Yes |