Write a program to check the presence of a file in the present working directory. Input has to be given at load time. (Command line Argument)
#include<stdio.h> int Check_Presence_of_File(char*); enum File_Status{IS_PRESENT,IS_NOT_PRESENT}; int main(int argc,char**argv) { if(argc != 2) { printf("Usage:./a.out filename\n"); return 0; } if(IS_PRESENT == Check_Presence_of_File(argv[1])) printf("File is present in this directory\n"); else printf("File is not present in this directory\n"); return 0; } int Check_Presence_of_File(char* p) { FILE *fp; fp = fopen(p,"r"); if(fp == NULL) return IS_NOT_PRESENT; else return IS_PRESENT; }
Output
./a.out data File is present in this directory
./a.out junk File is not present in this directory