Saturday, April 2, 2016

Program to write a text to a file in C


We will write a program in C that will accept a string from the user and write the string in a file. In this program, we will use fopen function in order to open a file. We will also use fputs function which will write a string to a file.


Below is the program:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<conio.h>
int main()
{
char *str;
FILE *fp;
//Open the file in write mode
fp = fopen("write.txt", "w");
printf("Enter a string: ");
gets(str);
        
        //write string in a file
fputs(str, fp);
getch();
}

0 comments:

Post a Comment