Ok so I'm new to C, this is the first time I've done anything with it. I have to create small compiler for a class, however before I get to doing that I need to check that the file that will be compiled exists, so I made a few functions to get a file path and then verify if the file exists. However, the access function seems to be telling me that my path does not exist even when it does. I'm using this on Linux. Here's my code so far:
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
int b_file_path;
char file_path[1024];
printf("Specify a valid file path to compile file: ");
fgets(file_path, sizeof(file_path), stdin);
b_file_path = validate_file_path(file_path);
while(b_file_path == 0){
printf("Invalid file path, please specify a valid file path: ");
fgets(file_path, sizeof(file_path), stdin);
b_file_path = validate_file_path(file_path);
}
return 0;
}
int validate_file_path(const char* path){
if(access(path, F_OK)!= -1){
return 1;
}
return 0;
}
I think the problem is happening with that pointer which is an argument within "validate_file_path", but I'm not sure. I also found out that the path had a different length when I just placed it withing the access function instead of using a variable. Can someone help me out? I'm not really sure about what to check.