Thursday, November 27, 2014

C programto display the content of the file .(accept filename through command line.)

/*...................................................................................................................
File :rec27.c
Program :To display the content of the file accept filename through command line.
Author :sujithbaby440@gmail.com
Date :26/11/2014
..................................................................................................................*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int ch;
if(argc<2){
printf("you have not entered the command properly, check the syntax\n");
printf("SYNTAX:stypefile<filename>\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("file does not exist");
exit(1);
}
printf("the content of the file are\n");
while((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 1;
}
Output :
[141740@localhost ~]$ ./a.out c27.c
the content of the file are
/*........................................................................
cmd.c
To display the content of the file accept filename through command line.
Author:141740
26/11/2014
.........................................................................*/
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
FILE *fp;
int ch;
if(argc<2){
printf("you have not entered the command properly, check the syntax\n");
printf("SYNTAX:stypefile<filename>\n");
exit(1);
}
if((fp=fopen(argv[1],"r"))==NULL){
printf("file does not exist");
exit(1);
}
printf("the content of the file are\n");
while((ch=getc(fp))!=EOF){
putchar(ch);
}
fclose(fp);
return 1;
}
[141740@localhost ~]$

No comments:

Post a Comment