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
Post A Comment:
0 comments: