This example writes a string to a stream.
#include <stdio.h>
#define FILENAME "myfile.dat"
#define NUM_ALPHA 26
int main(void)
{
FILE * stream;
int num;
/* Do not forget that the '\0' char occupies one character */
static char buffer[NUM_ALPHA + 1] = "abcdefghijklmnopqrstuvwxyz";
if ((stream = fopen("FILENAME", "w")) != NULL )
{
/* Put buffer into file */
if ( (num = fputs( buffer, stream )) != EOF )
{
/* Note that fputs() does not copy the \0 character */
printf( "Total number of characters written to file = %i\n", num );
fclose( stream );
}
else /* fputs failed */
perror( "fputs failed" );
}
else
perror( "Error opening FILENAME" );
return 0;
/*********************************************************************
The output should be:
Total number of characters written to file = 26 *********************************************************************/ }