May 6, 2020

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:

#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:



Share This:    Facebook Twitter

0 comments:

Post a Comment

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