Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

The GNU C Programming Tutorial - fputs

Node:fputs, Previous:puts, Up:Unformatted string output



fputs

The fputs ("file put string") function is similar to the puts function in almost every respect, except that it accepts a second parameter, a stream to which to write the string. It does not add a newline character, however; it only writes the characters in the string. It returns EOF if an error occurs; otherwise it returns a non-negative integer value.

Here is a brief code example that creates a text file and uses fputs to write into it the phrase If it's not too late... make it a cheeseburger., followed by a newline character. This example also demonstrates the use of the fflush function. (See Stream buffering, for more information on this function.)

#include <stdio.h>

int main()
{
  FILE *my_stream;
  char my_filename[] = "snazzyjazz.txt";
  int flush_status;

  my_stream = fopen (my_filename, "w");
  fputs ("If it's not too late... make it a cheeseburger.\n", my_stream);

  /*
     Since the stream is fully-buffered by default, not line-buffered,
     it needs to be flushed periodically.  We'll flush it here for
     demonstration purposes, even though we're about to close it.
  */
  flush_status = fflush (my_stream);
  if (flush_status != 0)
    {
      puts ("Error flushing stream!");
    }
  else
    {
      puts ("Stream flushed.");
    }


  /* Close stream; skip error-checking for brevity of example */
  fclose (my_stream);

  return 0;
}

 
 
  Published under free license. Design by Interspire