Write a program to implement cat command. Read content of file and display in on the screen.
#include<stdio.h> void Read_File(FILE*); int main() { char fileName[50]; FILE *fileptr; printf("Enter file name : "); scanf("%[^\n]",fileName); fileptr = fopen(fileName,"r+"); if(fileptr == NULL) { printf("%s File is not present in this directory\n",fileName); 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.
cat data Lot To Learn BlackBerry Iphone Incredible India
Output
Enter file name : data Lot To Learn BlackBerry Iphone Incredible India