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

StackOverflow https://stackoverflow.com/questions/19392507

  •  30-06-2022
  •  | 
  •  

Question

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;
    }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top