C program to Write to text file

Below is C program to Write to text file .

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
/* Write to text file */
 
#include<stdio.h>
int main()
{
 FILE *fp;
 char mystr[100];
 
 fp=fopen("mytext.txt","w");
 if(fp==NULL)
 {
  puts("Some error occured.");
  return 1;
 }
 
 printf("\nEnter some lines:\n");
 while(strlen(gets(mystr))>0)
 {
  fputs(mystr,fp);
  fputs("\n",fp);
 }
 fclose(fp);
 return 0;
}

No comments:

Post a Comment