Get file name from command line and open the file : File Open « File « C / ANSI-C






Get file name from command line and open the file

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

int main(void)
{
  FILE *fp;
  char fname[128];

  printf("Enter filename: ");
  gets(fname);

  if((fp=fopen(fname, "r"))==NULL) {
    printf("Cannot open file.\n");
    exit(1);
  }

  fclose(fp);

  return 0;
}


           
       








Related examples in the same category

1.Open a file and close it
2.Reopen a file
3. Open a file: how to use fopen
4. Reopen a stream with a different file and mode: how to use freopen
5.Open a file for read