Check that file exists: Difference between revisions

→‎{{header|C}}: Remove the alternate solution to the talk page.
(→‎{{header|C}}: Fix first example to use filenames from task description.)
(→‎{{header|C}}: Remove the alternate solution to the talk page.)
Line 237:
check_dir("/docs") ? "yes" : "no");
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>
 
Anonymous user