I am trying to create a program that using the
fopen();
function.My problem is that
fopen();
cannot find the file.- when i am using
perror("Error");
the output isError: No such file or directory.
- when i am using
I have read this articles and they did`not solved my problem:
- fopen() returning a NULL pointer, but the file definitely exists
- Unable to open a file with fopen()
- It means i have tried to use
fopen();
with the exec path to the file, and i have checked that the filename is the specific filename that i am looking for. - I have also used
exec("pwd");
to see that i am the correct directory.
My OS is ubuntu mate 20.04.
Here is my Code:
/* File Name: firstTransaction.c * File Mission: scan the input files and make the first transaction. */ #include "defines.h" /* * first transaction file: * Allocating new memory for the assembly source file. * Scanning the assembly source file. * @param filename - the filename of the file that needs to be converted by the program. * @return output and files - * output the assembly source file in a machine code. * return extern, entry and object files. */ /* function prototype */ extern char * readText(FILE * ptr, long ic, long dc); /* the function defined on assistanceFunctions.c file */ int firstTransaction(int * cf, char **av) { FILE * fp; /* a pointer for fopen function */ char * filteredFile = NULL; long ic = 100, dc = 0; /* declaration and initialization of the instruction counter and current data counter */ int i = 0, j = 0; /* indexes */ int cfh = 0; /* compatible file holder */ int fileNameLength = (int)strlen(av[i]); /* computing the first filename length to allocate memory */ char * fileName = (char*) calloc(fileNameLength,sizeof (char)); /* allocating memory for the first filename */ system("pwd"); while(cf[i] != 0) /* while there is more compatible files to open */ { cfh = cf[i]; /* cfh - compatible file holder, cf - compatible file array, set the next compatible file to cfh */ strcpy(fileName,av[cfh]); /* copy the file name from the argv array */ fp = fopen("fileName","r"); /* open the first compatible file for read */ if(fp == NULL) /* if file does not exists */ { perror("Error"); /* print the error - why the file not opened */ i++; /* increment i by one and try to open the next filename */ }else /* if the file was opened successfully, start scanning the file */ { /*filteredFile = */ readText(fp, ic, dc); /* calling readText function with pointer to the start of the file that was opend by fopen function */ /* while(filteredFile[j] != EOF) { putchar(filteredFile[j]); j++; } /* DONT FORGET TO CLOSE WITH FCLOSE(); */ } } return 0; }
Here is some screenshots with the tested solutions that failed from the other articles:
Edited after NeonFire answers:
- My first code used
fp = fopen(fileName,"r");
but my error message was made manually this way:printf("error, %s file does not exists\n", fileName); /* print error message to the user */
- this cause that i did`not find my real error.
- then i changed to
fp = fopen("fileName","r");
and usedperror("Error");
instead of the first and right way. - after i read NeonFire answers i got a new error Error: Too many open files.
- i removed the backups file from the directory and i still have the same error.
Edited after Ted Lyngmo comment: i did had to close the file to solve the - "to many file to open error".