Search code examples
cscanffseek

C : files manipulation Can't figure out how to simplify this code with files manipulation


I have been working on this code but I can't find out what is wrong. This program does compile and run but it ends up having a fatal error.

The program reads a file and collect the numbers in order to calculate the total (after having converted them to float). Then it reads the file and display the one that has less than 10.00

I have a file called myFile.txt, with the following content :

James------ 07.50 Anthony--- 17.00

So the display should be

  1. The total is 24.50
  2. Here is the one with less than 10.00 :
  3. James who has 07.50

And here is the code :

int main()
{
   int n =2, valueTest=0,count=0;
   FILE* file = NULL;
   float temp= 00.00f, average= 00.00f, flTen = 10.00f;
   float *totalNote = (float*)malloc(n*sizeof(float));

   int position = 0;
   char selectionNote[5+1], nameBuffer[10+1], noteBuffer[5+1];

   file = fopen("c:\\myFile.txt","r");
   fseek(file,10,SEEK_SET);
   while(valueTest<2)
   {
       fscanf(file,"%5s",&selectionNote);
       temp = atof(selectionNote);
       totalNote[position]= temp;
       position++;

       valeurTest++;
   }

   for(int counter=0;counter<2;counter++)
  {
          average += totalNote[counter];
  }
  printf("The total is : %f \n",average);

  rewind(file);
  printf("here is the one with less than 10.00 :\n");

  while(count<2)
  {
        fscanf(file,"%10s",&nameBuffer);

        fseek(file,10,SEEK_SET);
        fscanf(file,"%5s",&noteBuffer);
        temp = atof(noteBuffer);

        if(temp<flTen)
        { 
           printf("%s who has %f\n",nameBuffer,temp);
        }
        fseek(file,1,SEEK_SET);
        count++;
   }
   fclose(file);

}

I am pretty new to c and find it more difficult than c# or java. And I woud like to get some suggestions to help me to get better. I think this code could be simplier. Do you think the same ?


Solution

  • You don't mention what platform and compiler you're using. I'm going to guess MSVC and Windows, given that your filename uses a Windows-style path.

    You say that the code compiles and runs but have you turned on the warnings from your compiler? That might give you more clues about potentially incorrect things you may be doing.

    After fixing some of the typos and adding the standard include headers, I compiled your code in gcc which warned about mismatches between the fscanf format strings and the arguments. In particular, you are passing a pointer to the char arrays which is not what you mean. You may want to read up about arrays and pointers in C, e.g. in the C FAQ. The rest of the entries in that document are very enlightening as well.