Search C Program

Loading...

Write to text file

/* 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;
}

4 comments:

  1. Replies
    1. In C for File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. Here FP is FILE pointer..

      Delete
  2. Its very useful at any place to teach students

    ReplyDelete