Reputation: 69
I am writing a c file where the user runs it from the terminal (centos) and it gets on argument in argv, argv[1] = "FileName.txt". The code supposes to print the file's content only if the file does exist.
I am only allowed to use <fcntl.h> for that matter.
The code is:
int fd;
if((fd = open(path, O_CREAT | O_EXCL)) != -1){ //if file does not exist print and exit
free(path);
Error("File Not Found!\n");
}
char* path is already defined above. Error is a function that prints the message and then exits the program.
My problem is that it does work, but after the first run the open() function creates the file I have passed to it so when the user types again the file's name it passes through.
Going through the documentation I only found these flags to check if the file already exist. Does anyone know how to to check if a file exists without creating one?
Thank you.
Upvotes: 1
Views: 93