Search code examples
cpointersbufferstructurefgets

How to scan buffer, which is defines in structure, correctly?


This is my structure:

struct Data{
   char *buffer[MAXBUFF];
   int bufferSize;
};

This is how I call my function searchInFile:

searchInFile(logFile, outFile, &dat);

This is searchInFile function itself:

void searchInFile(FILE *logFile, FILE *outFile, struct Data *dat){
   FILE *srcFile = fopen("src.txt", "r");

   if(!srcFile){
      printf("Nepavyko atidaryti \"src.txt\" failo.\n");
      fprintf(logFile, "Nepavyko atidaryti \"src.txt\" failo.\n");
      fclose(srcFile);
      return;
   } else {
      fprintf(logFile, "Failas \"src.txt\" atidarytas sekmingai.\n");
   }

   while(!feof(srcFile)){
      fgets((*dat).buffer, MAXBUFF, srcFile);
      printf("%s", dat->buffer);
   }

   fclose(srcFile);
}

What I am trying to do is scan chars to a buffer, which is defined in structure. I get warning, which says that I pass argument of 'fgets' from incompatible pointer type. How to scan it correctly?


Solution

  • The first argument to fgets should be a char *.

    So you need to fix the structure definition from

    struct Data{
       char *buffer[MAXBUFF];
       int bufferSize;
    };
    

    TO

    struct Data{
       char buffer[MAXBUFF];
       int bufferSize;
    };