Next Previous Contents

38. fputs


Write a string to an open stream


Integer_Type fputs (String_Type s, File_Type fp);


The fputs function writes the string s to the open file pointer fp. It returns -1 upon failure and sets errno, otherwise it returns the length of the string.


The following function opens a file in append mode and uses the fputs function to write to it.

    define append_string_to_file (str, file)
       variable fp;
       fp = fopen (file, "a");
       if (fp == NULL) verror ("Unable to open %s", file);
       if ((-1 == fputs (s, fp))
           or (-1 == fclose (fp)))
         verror ("Error writing to %s", file);

One must not disregard the return value from the fputs function, as many C programmers do. Doing so may lead to a stack overflow error.

See Also

fclose, fopen, fgets

Next Previous Contents