Anonymous user
Check that file exists: Difference between revisions
→{{header|C}}: Fix first example to use filenames from task description.
(Added BBC BASIC) |
(→{{header|C}}: Fix first example to use filenames from task description.) |
||
Line 209:
=={{header|C}}==
{{
<lang c>#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
int main(){▼
/* Check for regular file. */
struct stat file_stat;▼
int check_reg(const char *path) {
return stat(path, &sb) == 0 && S_ISREG(sb.st_mode);
}
return 0;▼
/* Check for directory. */
int check_dir(const char *path) {
struct stat sb;
return stat(path, &sb) == 0 && S_ISDIR(sb.st_mode);
}
▲int main() {
printf("input.txt is a regular file? %s\n",
check_reg("input.txt") ? "yes" : "no");
printf("docs is a directory? %s\n",
check_dir("docs") ? "yes" : "no");
printf("/input.txt is a regular file? %s\n",
check_reg("/input.txt") ? "yes" : "no");
printf("/docs is a directory? %s\n",
check_dir("/docs") ? "yes" : "no");
}</lang>
|