[Solved] why do i get the error invalid conversion from ‘char’ to ‘const char”‘ in c language


Issues that I see:

Issue 1:

struct theFile{
    FILE *fPointer;
    char *fileItems[];
    int count;
}myFile;

is not valid. The flexible array member has to be the last member of the struct. Use

struct theFile{
    FILE *fPointer;
    int count;
    char fileItems[]; // This is an array of char not an array of char*.
}myFile;

instead.

Issue 2:

strcpy(myFile.fileItems[i], item);

is not valid since the second argument is of type char not char*. That’s what the compiler is telling you.

Issue 3:

Your code needs to be updated for the flexible way you want to keep adding input data to myFile.

void saveFile()
{
   int item;
   int i = 0;

   myFile.fPointer = fopen("mileage.txt", "r");

   // Deal with error condition.
   if ( myFile.fPointer == NULL )
   {
       // Add an appropriate error message.
       printf("Unable to open '%s' for reading.\n", "mileage.txt");
       return;
   }

   myFile.fileItems = malloc(i+1);

   while ((item = fgetc(myFile.fPointer)) != EOF )
   {
      if (item != ',' || item != ' ')
      {
         myFile.fileItems = realloc(myFile.fileItems, i+1);
         myFile.fileItems[i] = item;
         i++;
      }
   }
   myFile.count = i;

   // You need to call fclose(myFile.fPointer) somewhere.
   // I am not sure what's the best place in your program to do that.
   // This function might as well be that place.
   fclose(myFile.fPointer);
   myFile.fPointer = NULL;
}

Issue 4:

The name saveFile seems a bit misleading since you are not saving anything to a file. readFile sounds like a better name to me.

solved why do i get the error invalid conversion from ‘char’ to ‘const char”‘ in c language