Write a C program to read a file name at the command line and display its contents.




Algorithm:
1. Start
2. Read command line arguments
3. Check if no of arguments =2 or not. If not print invalid no of arguments
4. Open file in reading mode
5. Repeat until end of the file and print the contents if the file
6. Close the file
7. Stop




Program Code:
#include<stdio.h> 
void main(int argc,char * argv[]) 
FILE *fp; 
char ch;
fp=fopen(argv[1],"r");
if(fp==NULL) 
printf("\n opening problem"); 
return; 
while(!feof(fp)) 
ch=getc(fp); 
printf("%c",ch); 
fclose(fp); 
}





Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: