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

Posted by Bon_chan on Stack Overflow See other posts from Stack Overflow or by Bon_chan
Published on 2010-12-29T20:16:29Z Indexed on 2010/12/29 20:54 UTC
Read the original article Hit count: 251

Filed under:
|
|

Hey guys,

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.

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

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

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 ?

© Stack Overflow or respective owner

Related posts about c

    Related posts about fscanf