Program to Insert String in a file


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

main()
{
 FILE *fp;
 char s[100];
 clrscr();

 fp=fopen("Filename.extension","a");

 if(fp==NULL)
 {
 printf("cannot open the file");
 getch();
 exit(0);
 }

 printf("Enter the String\n");

 while(strlen(gets(s))>0)
 {
  fputs(s,fp);
  fputs("\n",fp);
 }

 fclose(fp);
 getch();
}


/*EXPLAINATION

fp is a pointer which  points to adress of the specified file.

"a":opens the file and insert new information without overwriting the existing

[if the file doesn't exits then 1st it'll be created then the records are inserted]

fputs takes two arguments

(1) Source(i.e. String here)
(2) Target(i.e. File Pointer)

[see the syntex accordingly]

fopen and fclose: Open and close the specified file

*/

0 comments:

Post a Comment