Fputs

From aldeid
Jump to navigation Jump to search

Syntax

int fputs ( const char * str, FILE * stream );

Description

Write string to stream

Writes the C string pointed by str to the stream.

The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This terminating null-character is not copied to the stream.

Notice that fputs not only differs from puts in that the destination stream can be specified, but also fputs does not write additional characters, while puts appends a newline character at the end automatically.

Parameters

str
C string with the content to be written to stream.
stream
Pointer to a FILE object that identifies an output stream.

Return Value

On success, a non-negative value is returned.

On error, the function returns EOF and sets the error indicator (ferror).

Example

#include <stdio.h>

int main ()
{
   FILE * pFile;
   char sentence [256];

   printf ("Enter sentence to append: ");
   fgets (sentence,256,stdin);
   pFile = fopen ("mylog.txt","a");
   fputs (sentence,pFile);
   fclose (pFile);
   return 0;
}

This program allows to append a line to a file called mylog.txt each time it is run.