incompatible pointer types passing 'char*' to parameter of type 'FILE*'(aka 'struct__sFILE*')

knrkn picture knrkn · Oct 16, 2013 · Viewed 9.3k times · Source

I have a frustrating problem. I am getting the following error:

incompatible pointer types passing 'char*' to parameter of type FILE*'(aka 'struct__sFILE*')".

Anyone know how I would fix this problem?

void load_myFile(char my_file_name[]) {
    if(my_file_name != NULL) {
        int op_code, L_code, M_code, i = 0;
        while(my_file_name != NULL) {
            fscanf(my_file_name, "%d", &op_code);

            if(i > MAX_CODE_LENGTH) {
                printf("Program is longer than MAX_CODE_LENGTH\n");
                exit(ERROR_PROG_TOO_LONG);
            }
            fscanf(my_file_name, "%d", &L_code);
            fscanf(my_file_name, "%d", &M_code);

            code[i].op = op_code;
            code[i].l = L_code;
            code[i].m = M_code;
            i++;
        }
        code_size = i;
    }

Answer

Tu Bui picture Tu Bui · Oct 16, 2013

According to your declaration, my_file_name is a string of char, not a pointer to FILE. While function fscanf requires a pointer to FILE. Therefore you got that kind of error.

If you want it to be a string, use sscanf instead of fscanf to extract data. Otherwise, declare a FILE pointer, open a file and read from it.