-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexists.c
More file actions
55 lines (46 loc) · 982 Bytes
/
exists.c
File metadata and controls
55 lines (46 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Analogous to fs::exists(filepath);
//Extracts the file name from the silepath and checks for the existence of the file.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#ifdef _WIN32
#include <Windows.h>
#endif
#ifdef _WIN32
const char SEP = '\\';
#else
const char SEP = '/';
#endif
bool exists(char filepath[])
{
char *temp;
char *p = filepath;
while(*(p + 1) != '\0' && (p = strchr(p + 1, SEP)) != NULL)
{
temp = p;
}
temp++;
if( access(filepath, F_OK ) != -1 )
{
return true;
}
else
{
return false;
}
}
int main()
{
char filepath[10000] = {'\0'};
scanf("%s", filepath);
if(exists(filepath))
printf("The file exists.\n");
else
printf("The file doesn't exist.\n");
return EXIT_SUCCESS;
}