C++ fputs()

The fputs() function in C++ writes a string completely except the terminating null character to the given output file stream.

It is same as executing fputc() repeatedly.

fputs() prototype

int fputs(const char* str, FILE* stream);

The fputs() function writes all the character stored in the string str to the output file stream except the terminating null character.

It is defined in <cstdio> header file.

fputs() Parameters

  • str: Pointer to an character array that stores the string to be written.
  • stream: The output file stream to write the characters.

fputs() Return value

On success, the fputs() function returns a non-negative value. On failure it returns EOF and sets the error indicator on stream.

Example: How fputs() function works

#include <cstdio>

int main()
    char str[] = "Learning to program";
    FILE *fp;
    fp = fopen("file.txt","w");
    if (fp)
        perror("File opening failed");
    return 0;

When you run the program, it will write "Learning to program" to the file file.txt.

Did you find this article helpful?