Command line arguments in C
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fp;
char c;
if(argc==2)
{
fp=fopen(argv[1],"w");
while((c=getchar())!=EOF)
{
putc(c,fp);
}
fclose(fp);
fp=fopen(argv[1],"r");
while((c=getc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
}
return 0;
}
How to run this program in linux?
I have tried these steps:
- open terminal in linux
- type the command ./a.out program_name file_name