0
  1. I am trying to create a program that using the fopen(); function.

  2. My problem is that fopen(); cannot find the file.

    • when i am using perror("Error"); the output is Error: No such file or directory.
  3. I have read this articles and they did`not solved my problem:

  4. My OS is ubuntu mate 20.04.

  5. 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:

triedSolutions

Edited after NeonFire answers:

  1. 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 used perror("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".

CrazyTux
  • 204
  • 2
  • 12
  • 5
    your ```fp = fopen("fileName","r");``` is incorrect. Remove the quotes from fileName so it refers to your variable ```char * fileName``` , not a string ```"fileName"``` – NeonFire Aug 11 '21 at 18:39
  • 2
    @NeonFire Make it into an answer. Would get my vote. – Ted Lyngmo Aug 11 '21 at 18:41
  • 2
    @TedLyngmo simple mistakes always get the best of us! – NeonFire Aug 11 '21 at 18:44
  • 1
    I think that the OP might well have discovered the root cause if they'd made more of an effort to reduce this code down to a minimum reproducible example. – Tim Randall Aug 11 '21 at 18:53
  • What does "_Here is some screenshots with the tested solutions that failed from the other articles:_" have to do with the original question? **One** question per question please. – Ted Lyngmo Aug 11 '21 at 18:53
  • 1
    The _backups_ do not have anything to do with this. You open a lot of files but you don't close them so ... "_Too many open files_" is the result. `fclose` each `FILE*` when you're done with it. – Ted Lyngmo Aug 11 '21 at 18:55
  • "Here is some screenshots" Please do not add screenshots of plain text. Instead just copy&paste into your question. – Gerhardh Aug 11 '21 at 19:30
  • `calloc(fileNameLength,` That is not enough to hold the terminating 0 byte. Also in C it is not needed to cast result of `malloc`, `calloc` etc.. – Gerhardh Aug 11 '21 at 19:31
  • 1
    You don't allocate enough memory to hold the filename string. Remember that strings are zero-terminated. You also assume that no filenames are longer than the first one when doing `strcpy`. And the allocated memory is never freed. In your case you don't need to mess with `calloc`/`strcpy`/`free`. Just let `fileName` point to the same strings as `av[cfh]`, like this: `fileName = av[cfh];` – HAL9000 Aug 11 '21 at 19:36
  • Hi there and thanks for your comment, i am aware to this issues, i stopped editing the code while getting the question errors. – CrazyTux Aug 11 '21 at 19:42

1 Answers1

7

your fp = fopen("fileName","r"); is incorrect. Remove the quotes from fileName so it refers to your variable char * fileName , not a string "fileName" :)

NeonFire
  • 186
  • 2
  • 7
  • hi there and thanks for your answer! i first tried with `fileName` without quotes but the error message was made by me with `printf();` function, that's why i didn't noticed the other error that i have which seems to be "Error: Too many open files", i have a backups directory that contains all the files that i have in the parent directory, ill try to delete now the backups file, maybe it is trying to read Recursively. – CrazyTux Aug 11 '21 at 18:48
  • 3
    @CrazyTux That's an unrelated issue and you have a hint in your own code `DONT FORGET TO CLOSE WITH FCLOSE();` (it should be `fclose(FILE*);` but close enough) - However, this answer answers your question about not being able to open the file. – Ted Lyngmo Aug 11 '21 at 18:49