Reputation: 5297
I need to ask you about a better way to check if a file exists or not, because i really do not know which is the right way.
i have a program which works just fine, but the problem appears when i have to deal with different kind of files, not just, lets say txt
files.
I am a Linux user and i tried to see how the program works if i try to use it on a Library file.
Here is the problem, the program cannot read that file because i use READ MODE "r"
, and the library file is not a txt
file.
How can i check if that file exist ?
#include <stdio.h>
#include <stdlib.h>
int main (void){
FILE * fp;
char *fileNAme = "secret.txt";
size_t fileLong;
char * buffer;
size_t result;
/*Check if the file exists, if not Exit*/
fp = fopen (fileNAme , "r");
if (fp==NULL){
printf("\n\n\t\t\tThe file %s does not Exists\n\n\n\n\n", fileNAme);
exit(1);
}
/* Read the Number of Chars present and store them in FileLong*/
fseek (fp , 0 , SEEK_END);
fileLong = (size_t)ftell (fp);
/* Print the size of the FileLong */
if(fileLong == 0){
printf("The Size of the File is \t= %ld\n",fileLong);
}else{
printf("The Size of the File is \t= %ld\n",fileLong-1);
}
/* Moving the pointer back to the begining of the file */
rewind (fp);
/* allocate memory to the buffer to which is needed for holding the whole File content */
buffer = malloc (sizeof(char)*fileLong);
/* Check if the memory was aloccated, if not print the Error and Exit */
if (buffer == NULL){
fputs ("Memory error",stderr);
exit (1);
}
/* copy the file into the buffer:*/
result = fread (buffer,1,fileLong,fp);
/*Print the size of the Buffer*/
if(result == 0){
printf("The size of the Buffer is \t= %ld\n",result);
}else{
printf("The size of the Buffer is \t= %ld\n",result-1);
}
/* Check if the Buffer have the same size as lsize */
if (result != fileLong){
fputs ("Reading error",stderr);
exit (1);
}else{
printf("\nThe Content of the File(%ld)\tis the same as the content of the Buffer(%ld)\n", fileLong-1, result-1);
}
/* Now we can print the Content of the File */
printf("\n\n\n\n\t\tThe Content of the File is:\n");
/* Compare result with FileLong to see if there are some information inside that File or not */
if(result != 0 && fileLong != 0){
printf("%s", buffer);
}else{
printf("\n\n\nThe File is empty\n\n\n");
}
/* Close that File */
fclose (fp);
/* Free the needed memory*/
free (buffer);
return 0;
}
Thank you all.
Upvotes: 0
Views: 264
Reputation: 5297
I will post a part of my solution.
It is a Linux work out, because I'm not a Windows User and for my needs it is enough to check if a file exists in C using access
.
Here is the Code:
#include <stdio.h>
#include <stdlib.h>
#include<unistd.h>
void fileCheck(const char *fileName){
if(!access(fileName, F_OK )){
printf("The File %s\t was Found\n",fileName);
}else{
printf("The File %s\t not Found\n",fileName);
}
if(!access(fileName, R_OK )){
printf("The File %s\t can be readed\n",fileName);
}else{
printf("The File %s\t cannot be readed\n",fileName);
}
if(!access( fileName, W_OK )){
printf("The File %s\t it can be Edited\n",fileName);
}else{
printf("The File %s\t it cannot be Edited\n",fileName);
}
if(!access( fileName, X_OK )){
printf("The File %s\t is an Executable\n",fileName);
}else{
printf("The File %s\t is not an Executable\n",fileName);
}
}
int main (void) {
char *fileName = "/boot/grub/grub.cfg";
fileCheck(fileName);
return 0;
}
Output:
The File /boot/grub/grub.cfg was Found The File /boot/grub/grub.cfg can be readed The File /boot/grub/grub.cfg it cannot be Edited The File /boot/grub/grub.cfg is not an Executable
Upvotes: 0
Reputation: 134316
You can use the access()
API with F_OK
to check the existence of the file.
You need to include the header file unistd.h
.
FWIW, you can also use R_OK
, W_OK
, and X_OK
to check whether the file got read, write and execute permission, additionally.
Here is how you could use access
:
if( !access( filename, F_OK ))
{
// file is present
}
else
{
// file is not there
}
Upvotes: 3
Reputation: 409166
It doesn't matter what type of file it is, fopen
will still return a valid FILE
pointer if it can be opened. So one easy way is simply to use fopen
and see if it returns NULL
or not.
Another, more Linux (or rather POSIX) way may be to use the access
function, or possibly the stat
function.
Upvotes: 3