fputs() — Write String

Format

#include <stdio.h>
int fputs(const char *string, FILE *stream);

Language Level: ANSI

Threadsafe: Yes.

Description

The fputs() function copies string to the output stream at the current position. It does not copy the null character (\0) at the end of the string.

Return Value

The fputs() function returns EOF if an error occurs; otherwise, it returns a non-negative value.

The fputs() function is not supported for files that are opened with type=record.

For information about errno values for fputs(), see fputc() — Write Character.

Example that uses fputs()

This example writes a string to a stream.

#include <stdio.h>
 
#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("mylib/myfile", "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 myfile" );
}

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]