Check that file exists: Difference between revisions
Content added Content deleted
(→{{header|C}}: added alternate c code) |
|||
Line 195: | Line 195: | ||
printf("no file lol"); |
printf("no file lol"); |
||
return 0; |
return 0; |
||
}</lang> |
|||
Or use this way |
|||
<lang c>#include<stdio.h> |
|||
#include<dir.h> |
|||
#include<errno.h> |
|||
signed int fexist(char*s){ |
|||
FILE*f=fopen(s,"r"); |
|||
if(!f)return (errno==ENOENT)-1; |
|||
fclose(f); |
|||
return 1; |
|||
} |
|||
signed int direxist(char*s){ |
|||
if(chdir(s))return (errno==ENOENT)-1; |
|||
return 1; |
|||
} |
|||
void report(char*name,signed int r){ |
|||
char*s="might"; |
|||
if(r>0)s="does"; |
|||
else if(!r)s="does not"; |
|||
printf("%s %s exist.\r\n",name,s); |
|||
} |
|||
void chkfile(char*s){ |
|||
report(s,fexist(s)); |
|||
} |
|||
void chkdir(char*s){ |
|||
report(s,direxist(s)); |
|||
} |
|||
int main(){ |
|||
chkfile("input.txt"); |
|||
chkfile("/input.txt"); |
|||
chkdir("docs"); |
|||
chkdir("/docs"); |
|||
return 0; |
|||
}</lang> |
}</lang> |
||