Tuesday, 9 December 2014

C Program to read a source file by using fgets

Program to read a file by using fgets
#include"myHeader.h"
main(int argc,char **argv)
{
char ch[20];
FILE *fp;
if(argc!=2)
printf("enter correct no of arguments\n");

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("no file exists\n");
return;
}
while((fgets(ch,20,fp)))
printf("%s",ch);
fclose(fp);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter correct no of arguments
no file exists
madan@madan-Lenovo-G570:~/madan$ ./a.out 1.c
#include<stdio.h>
main()
{
 int ch;
printf("enter any character:");
ch=getchar();
printf("entered character:");
putchar(ch);
}