This program will give you the idea.
Code:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fp;
if (argc!=2)
{
printf("USAGE: %s <file name>\n",argv[0]);
exit(0);
}
if ((fp=fopen(argv[1],"ra")) == NULL)
{
printf("File Does not exists. Creating one...");
fp=fopen(argv[1],"w");
if (fp!=NULL)
{
printf("successful.\n");
fclose(fp);
} else {
printf("failed.\n");
}
} else {
printf("File exists.\n");
fclose(fp);
}
return 0;
}
Sample Output
$ ./file1.exe demo.txt
File Does not exists. Creating one...successful.
$ ./file1.exe demo.txt
File exists.