C Program to Create a File and Write Data in it using fputs() and fputc() Functions

In this tutorial we will learn how to create a file and write data in it....

EX: Write a C program to create a file and write data in it, How to write to a file in C. C program to write data into a file.


  Program using fputs funtion:


#include<stdio.h>
void main()
{
    FILE *fp;

    fp = fopen("file.txt", "w");

    fputs("Welcome to C file handling tutorials......", fp);
    fclose(fp);
}



In above program we used fputs() to write data in a file, fputs() function is used to write string in file and if we use fputc() function then we can able to write only one character in file.


If we will open the file.txt file then we will see the following output:

c file handling tutorials, C program to write content in file, c file handling exercises

  •     Above same program using fputc() function:

#include<stdio.h>
void main()
{
    FILE *fp;

    fp = fopen("file.txt""w");

    fputc('a'fp);
    fclose(fp);
}


If we will open the file.txt file then we will see the following output:

C program to create a file and write data in it using fputs and fputc functions




If you have any doubts please discuss it in the comment section given below. Happy Coding...🙂


No comments:

Post a Comment

If you have any doubts, please discuss here...👇