C fputs() function is implemented in file-related programs for writing strings to any particular file. This tutorial guides you on how to use the fputs() function in the C program.



This function is declared in the stdio.h header file and has the following prototypes:

Syntax:

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

The fputs function takes two arguments: a pointer to a string 'str' and a pointer to a FILE stream. The string is written to the stream, except for the null character that terminates the string.

Here is an example of how fputs can be used to write a string to a file:

Example:

#include<stdio.h> 
int main()
{
    FILE *fp;
    fp = fopen("fileName.txt","w");

    fputs("This is a sample text file.", fp);
    fputs("This file contains some sample text data.", fp);

    fclose(fp);
    return 0; 
}

This example opens the file "fileName.txt" in write mode and writes some sample text. After this, the file is closed.

Note that fputs return a non-negative value on success or EOF on error. It is a good idea to check the return value to ensure that the string was written successfully.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram