Monday, February 1, 2010

puts() Vs fputs()


Write string to stdout puts():
Writes the C string pointed by str to stdout and appends a newline character ('\n').
The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to stdout.

Using fputs(str,stdout) instead, performs the same operation as puts(str) but without appending the newline character at the end.

/* puts example : hello world! */
#include 

int main ()
{
  char string [] = "Hello world!";
  puts (string);
}

Return Value

On success, a non-negative value is returned.
On error, the function returns EOF.


Write string to stream fputs():
Writes the 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 final null-character is not copied to the stream.
/* fputs example */
#include < stdio.h >

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

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

Return Value

On success, a non-negative value is returned. On error, the function returns EOF.


No comments:

Post a Comment