Fopen typecast for comparison with NULL

Asked

Viewed 65 times

1

When I make a fopen, I always use an if for error correction.

The pointers are declared in

FILE *mestre, *indice;

And I’m wearing it like this

if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) || ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
                printf("Erro na abertura do arquivo");              
        }

But I always get this Warning:

"Warning: assignment makes Pointer from integer without a cast"

Is there a Typecast that can treat this?

  • You know the function fopen returns a pointer to the structure FILE no? Take a look at the documentation: http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html. Maybe you are confusing this with the function open of fcntl.h that returns the File Descriptor(http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_075.htm).

  • Yes, I just forgot to comment there, the master and Dice are declared as FILE *master, *Input and correctly receiving the file, I would just like to know why Warning. including in the documentation has an example of what I am doing , at the end in Opening a file.

2 answers

2

You missed a parenthesis. Clang’s message is a little clearer:

k. c:6:86: Warning: incompatible integer to Pointer Conversion assigning to FILE * (aka struct _IO_FILE *) from int [-Wint-conversion]

...|| ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
               ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1 Warning generated.

Note the expression:

((a = fopen(...) == NULL))   // Em C, boleanos são int

I imagine it should be:

((a = fopen(...)) == NULL)

1


I believe the problem is how you parented the expression. I made a code with several attribution combinations, the problem in your case is that the second part of the expression was indice = fopen(...) == NULL and not (indice = fopen(...)) == NULL.

So by priority, your expression would be equal to indice = (fopen(...) == NULL). And there, you’re assigning an integer value to the pointer for the structure FILE, generating the error.

Below follows the code tested:

#include <stdio.h>

int main(void) {
    FILE* mestre, *indice;

    mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab");
    indice = fopen("//home//vitor//Desktop//indice.bin", "ab");

    if (mestre == NULL || indice == NULL){ 
        printf("Erro na abertura do arquivo");
    }

    if((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab")) == NULL) {
        printf("Erro na abertura do arquivo");
    }

    if(((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab")) == NULL) ||
    ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")) == NULL)) {
        printf("Erro na abertura do arquivo");
    }

    if (((mestre = fopen("//home//vitor//Desktop//mestre.bin", "ab"))==NULL) ||
       ((indice = fopen("//home//vitor//Desktop//indice.bin", "ab")==NULL))){ 
             printf("Erro na abertura do arquivo");              
    }

    return 0;
}
  • Gosh, thank you very much. I had not attacked myself to this very detail.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.