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;
}