Write and Read String from File using Command Prompt

#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
  FILE *fp1;
  char *s,ch;
  int i;
  clrscr();
  if(argc>=1)
   {
     fp1=fopen(argv[1],"w");
   }
  else
   {
     printf("\n no command line argument ");
     getch();
     exit();
   }
  for(i=2;i<argc;i++)
  {
      fprintf(fp1,"%s",argv[i]);
      if(i<argc-1)
          fputs("\n",fp1);
  }
  fclose(fp1);
  if((fp1=fopen(argv[1],"r"))==NULL)
   {
      printf("\n Error " );
      getch();
      exit(0);
   }
   while(!(feof(fp1)))
    {
       fscanf(fp1,"%s",s);
       printf("\n%s",s);
    }
   getch();
  }

Post a Comment

Previous Post Next Post