In this program, we are going to share how to write a sentence to a file using C programming language. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to write a sentence to a file with the output.
Copy the below C program and execute it with the help of Turbo C compiler. At the end of this program, We have shared the output of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <stdlib.h> int main() { char sentence[1000]; FILE *fptr; fptr = fopen("program.txt", "w"); if(fptr == NULL) { printf("Error!"); exit(1); } printf("Please enter a sentence:\n"); gets(sentence); fprintf(fptr,"%s", sentence); fclose(fptr); return 0; } |
Please enter a sentence:
I am trying to learn c programming.
Liked this program? Do Like & share with your friends.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: c program to read a file, c program to read a string of text from file, c program to read and write to a file, c program to write data into a file, c programs, c programs with output, c programs with solutions, list of important c programs, writing to a file in c