Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to write in a file using fputs() in C

  • Difficulty Level : Easy
  • Last Updated : 10 Jul, 2021

fputs() is a function declared in stdio.h header file. It is used to write the contents of the file. The function takes 2 arguments. The first argument is a pointer to the string which is to be written and the second argument is the name of the file where the string is to be written. It returns 1 if the write operation was successful, otherwise, it returns 0. fputs() writes a single line of characters in a file.


We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!

fputs(const *char str, FILE *fp);
where str is a char name that we write in a file and fp is the file pointer.


Input- str1 = “geeksforgeeks”, str2 = “gfg”
Output- The output file will consist of two lines:

Below is the C program to implement the fputs() function-


// C program to implement
// the above approach
#include <stdio.h>
#include <string.h>
// Function to write
// string to file
// using fputs
void writeToFile(char str[])
    // Pointer to file
    FILE* fp;
    // Name of the file
    // and mode of the file
    fp = fopen("f1.txt", "w");
    // Write string to file
    fputs(str, fp);
    // Close the file pointer
// Driver Code
int main()
    char str[20];
    strcpy(str, "GeeksforGeeks");
    return 0;


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!