Write a program and print content of file on the screen. Input file has to be supplied at load time.
#include<stdio.h> void Read_File(FILE*); int main(int argc,char**argv) { if(argc != 2) { printf("Usage:./a.out filename\n"); return 0; } FILE *fileptr; fileptr = fopen(argv[1],"r+"); if(fileptr == NULL) { printf("%s File is not present in this directory\n",argv[1]); return 0; } Read_File(fileptr); return 0; } void Read_File(FILE *p) { char ch; while((ch = fgetc(p)) != EOF) { printf("%c",ch); } fclose(p); }
Note: File name data is present in the current working directory.
It contains data as
cat data Lot To Learn BlackBerry Iphone Incredible India
Output
./a.out Usage:./a.out filename
./a.out data Lot To Learn BlackBerry Iphone Incredible India