Fputs() in C Programming Language

Fputs() in C


Standards

Standards / Extensions

C or C++

Dependencies

ISO C
POSIX.1
XPG4
XPG4.2
C99
Single UNIX Specification, Version 3
Language environment

both  

Format

#include <stdio.h>

int fputs(const char * __restrict__string, FILE * __restrict__stream);

#define _OPEN_SYS_UNLOCKED_EXT 1
#include <stdio.h>

int fputs_unlocked(const char * __restrict__string, FILE * __restrict__stream);

 

General description

Writes the string pointed to by string to the output stream pointed to by stream. It does not write the terminating \0 at the end of the string.

For a text file, truncation may occur if the record is too long. Truncation means that excess characters are discarded after the record is full, up to a control character that ends the line (\n). Characters after the \n start at the next record. For more information, see the topic on “Truncation” in z/OS XL C/C++ Programming Guide.

fputs() is not supported for files opened with type=record or type=blocked.

fputs() has the same restriction as any write operation for a read immediately following a write or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must also be an intervening flush or reposition unless an EOF has been reached.

fputs_unlocked() is functionally equivalent to fputs() with the exception that it is not thread-safe. This function can safely be used in a multithreaded application if and only if it is called while the invoking thread owns the (FILE*) object, as is the case after a successful call to either the flockfile() or ftrylockfile() function.

Returned value

If successful, fputs() returns the number of bytes written.

If unsuccessful, fputs() returns EOF.

Example

CELEBF35

/* CELEBF35                                      

   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("myfile.dat", "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 );