C Program to Read from Standard Input and Write Directly to File


In this post, there is a c program to read data from standard input and write that data directly to the file. In other words, we will read data from the console using getchar() function. For writing data to the file, I will use the fputc() function.

The getchar() function reads only a single character at one time from the console. The fputc() writes a single character to the respective file. See the following expected output.

Expected Output

Enter Data
Hello, this is vijay londhe. Whatever I am writing on the console, it will be written to the file.
Data is successfully written into the file

As you can see the above expected output. Here, we are asking user to enter the data. After pressing the enter key, the data will be written into the file. This is a very small program. Now, see the following program.

C Program to Read from Standard Input and Write Directly to File

#include <stdio.h>
#include <stdlib.h>
int main()
    FILE *fp;
    char ch;
        printf("Error while opening a file");
        return 0;
    printf("Enter Data\n");

    printf("Data is successfully written into the file");
    return 0;

I hope you have understood this program. If you have any doubt or any feedback, then please feel free the contact me.

Thank you.

Important Programs

  1. Interview Questions On C
  2. Switch Case in C Program to Calculate Area of Circle and Triangle
  3. C Language Program to Count the Number of Lowercase Letters in a Text File
  4. Program in C to Replace Capital C with Capital S in a File
  5. C Program to Remove White Spaces and Comments from a File
  6. Perfect Number in C Programming using All Loops
  7. Reverse a Number in C
  8. Factorial Program in C Programming
  9. LCM of Two Numbers in C Programming
  10. GCD of Two Numbers in C Programming
  11. Switch Case in C Programming
  12. C Program to Display Numbers From 1 to n Except 6 and 9
  13. C Program to Count the Characters in the String Except Space
  14. Strong Number in C Programming
  15. Swapping of Two Numbers in C
  16. The while loop in C Programming
  17. C Program to Find the Sum of Cubes of Elements in an Array
  18. C Program to Print Numbers Except Multiples of n
  19. Floyd Triangle in C Programming
  20. Operators in C with Detailed Explanation
  21. Print 1 to 50 using do while loop in Java
  22. C Program to Print Multiples of 5 using do while loop
  23. Palindrome in C using Pointers
  24. Arrays in C for Complete Beginners
  25. Insert and Delete element in Array in C using switch case
  26. Programs on Arrays in C
  27. C Program to Copy the Contents of One File into Another File
  28. C Program To Print All The Numbers In a Given Range
  29. Program to Display a String in Capital Letters
  30. C Program to Remove Zeros from a number

Leave a Comment