Thread: Manipulating a read-in file in C

  1. #31
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.2
    You just need to check fscanf() return value for EOF;
    My 945 page textbook has exactly 2 sentences on feof(), which really don't appear to help. I am going to try to get this without bothering you every 15 minutes until I have something substantial to show. Frustated. Thank you Tater
    Read the manual!
    Just google for man fscanf.

  2. #32
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JoshD75 View Post
    Yea, thank you Tater. That is pretty darn obvious after seeing it in code and reading it line by line....works perfectly. However it does NOT print the last line twice, at least on my end....
    Ok just to be clear... it gets all three lines of our test file correctly?

    Attachment 10548

    This is important before we move on...

    Ok so next step is to calculate the weighted averages as you said. Of course I am still game, and I appreciate you taking the time to explain this to me step by step. Much better than someone doing it for me, or guessing! Ok, so any hints as to the next step? Thank you again, this is invaluable to me as a learning experience...
    No worries... Now I need to know how you're calculating the averages ... the obvious way is to add everything up and divide... but you already explained it's not quite that obvious, so what's the formula?

  3. #33
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Thanks for the suggestion Bayint... but there's a reason not to do it that way just yet...

  4. #34
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Quote Originally Posted by CommonTater View Post
    Ok just to be clear... it gets all three lines of our test file correctly?

    Attachment 10548

    This is important before we move on...



    No worries... Now I need to know how you're calculating the averages ... the obvious way is to add everything up and divide... but you already explained it's not quite that obvious, so what's the formula?
    Correct Tater, all 3 lines of data show up exactly like yours shown above.

    The formula is q1-q4 are each valued at .10%, midterm is .25% and final is .35%

    Thanks again...

  5. #35
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... don't disturb your working code just yet... lets get the formula together first...
    So how would you write that in C?

    Hint: Try a couple of different versions and go with the simplest one that gives a correct answer.
    Last edited by CommonTater; 04-29-2011 at 10:10 AM.

  6. #36
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    I see you edited out your "try to do this with integers if you can". That confused me originally, as the result would be in Float I would think, "85.50" or whatever. I am going to work on this later and get back to you. Again, thank you!

  7. #37
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by JoshD75 View Post
    I see you edited out your "try to do this with integers if you can". That confused me originally, as the result would be in Float I would think, "85.50" or whatever. I am going to work on this later and get back to you. Again, thank you!
    Yeah... I was messing with it here and realized (too late) there was no simple way to do it with integers.

    Sorry...

    FWIW... I did manage to do it in a single line, with a float average variable using the ints from the file...
    Last edited by CommonTater; 04-29-2011 at 11:15 AM.

  8. #38
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Hi Tater, sorry for the delay. I have been in and out and not by my computer much. This is what I have thus far, and it is obviously not correct. I created a variable grade as float and did the calculation. Am I on the right path here? I know I need the average for each person, and this is not it..I think what this is doing is adding ALL the quiz1's together, ALL the quiz2's....etc etc...and diving them by 6..I'm getting 180.58 as an answer....hmmm....

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
      
      char firstname [50];
      char lastname [50];
      int q1,q2,q3,q4,mid,final;
      float grade;
      FILE *fp;
      char my_filename_input[] = "C:\\Documents and Settings\\xxxx\\Desktop\\Final_Input.txt";
    
      fp=fopen (my_filename_input, "r");
    
      while (! feof(fp))
      {  
      fscanf (fp,"%s %s %d,%d,%d,%d,%d,%d",firstname,lastname,&q1, &q2, &q3,&q4,&mid,&final);
      printf ("%s %s %d %d %d %d %d %d\n", firstname,lastname,q1, q2, q3,q4,mid,final);
      }
      {
    	  grade = ((q1*.10 + q2*.10 + q3*.10 + q4*10 + mid*.25 + final*.35) / 6);
    	  printf("%.2f", grade);
      }
      fclose(fp);
      return 0;
    }
    Last edited by JoshD75; 04-30-2011 at 03:49 PM.

  9. #39
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Wow... that's almost exactly what I had...
    But there's a little foible about floating point math using integers... you should use, for example (q1 * 0.1) + ... It may be compiler dependent but it seems to want that 0 in there. Also you would get the right answer if you didn't divide by 6.

    I tend to write paranthetic code, preferring to keep control over the order of things, so my version looks like this...

    average = (q1 * 0.1) + (q2 * 0.1) + (q3 * 0.1) + (q4 * 0.1) + (mid * 0.25) + (final * 0.35);

    This asks the compiler to to the math in the brackets first, then add them up.

    Now you need to put the calculations into the loop reading the file. Where it is now it will only be done once, not every time... we have to do every line... Move the calculaition up between the scanf() and the original printf() then modify the original printf() to also display the average.... Then run it with our test file and see what you get...

    Once you have that working, we kill that bug I mentioned...
    Last edited by CommonTater; 04-30-2011 at 04:56 PM.

  10. #40
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Got it! I think? I am going out for a bit and will check your response when I get back. Thank you again!

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
      
      char firstname [50];
      char lastname [50];
      int q1,q2,q3,q4,mid,final;
      float grade;
      FILE *fp;
      char my_filename_input[] = "C:\\Documents and Settings\\denauljo\\Desktop\\Final_Input.txt";
    
      fp=fopen (my_filename_input, "r");
    
      while (! feof(fp))
      {  
      fscanf (fp,"%s %s %d,%d,%d,%d,%d,%d",firstname,lastname,&q1, &q2, &q3,&q4,&mid,&final);
      grade = (q1 * 0.1) + (q2 * 0.1) + (q3 * 0.1) + (q4 * 0.1) + (mid * 0.25) + (final * 0.35);
      printf("%s %s %.2f\n", firstname,lastname, grade);
     
      // printf ("%s %s %d %d %d %d %d %d\n", firstname,lastname,q1, q2, q3,q4,mid,final);
      }
    
      fclose(fp);
      return 0;
    }

  11. #41
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Excellent... i would have done it a little differently...
    Code:
    printf ("%s %s %d %d %d %d %d %d  Average = %.2f\n", firstname,lastname,q1, q2, q3,q4,mid,final, grade);
    
    //printf("%s %s %.2f\n", firstname,lastname, grade);
    What you have isn't wrong by any means... I just like to see all the information on screen while debugging.
    We'll need that commented out line, so don't delete it....

    So... before we move on to the next step, do a few runs with our test file, make sure the results are consistent... and then we'll go after that bug I mentioned.

  12. #42
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    Works fine Tater, I added some names and some scores and it works as intented. To mix it up a bit, let me tell you what I think needs to happen next and you can tell me if I am way off or on the right track. The next step (besides the bug you mentioned) I believe is to add an if else() loop statement beneath arithmetic line, breaking it down into letter grades. 70-79, 80-89, 90-100 = C, B , A or whatever.

    Is that the next step? Once that is complete then we configure the output file?

    Thanks

  13. #43
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yeah we can do letter grades next if you like. In fact, the bug can (should?) wait till we're done with that --even till after we do the file write since it involves all the steps.

    So show me how you'd do letter grades, without disturbing the working code we have now...

  14. #44
    Registered User
    Join Date
    Mar 2011
    Posts
    42
    I am getting a syntax error, but I think it should be something along the lines of.....

    Code:
      while (! feof(fp))
      {  
      fscanf (fp,"%s %s %d,%d,%d,%d,%d,%d",firstname,lastname,&q1, &q2, &q3,&q4,&mid,&final);
      grade = (q1 * 0.1) + (q2 * 0.1) + (q3 * 0.1) + (q4 * 0.1) + (mid * 0.25) + (final * 0.35);
    	if (grade >89 &&<101)
    		printf("%s %s",firstname, lastname, 'A');
     // printf("%s %s %.2f\n", firstname,lastname, grade);
     
      // printf ("%s %s %d %d %d %d %d %d\n", firstname,lastname,q1, q2, q3,q4,mid,final);
      }
    Last edited by JoshD75; 05-01-2011 at 12:19 PM.

  15. #45
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... Remember we need this in a variable because we need it more than once.
    (Once for the screen, once for the file)

    If you print it to the screen as you're doing now... it's gone, never stored.

    Worry about the math...

    Code:
    char eggsize;
    
    if (diameter > 100)
      eggsize = 'A';
    else if (diameter > 80)
      eggsize = 'B';
    else if (diameter >30)
      eggsize ='C';
    else
      eggsize = 'D';
    Add the result to our original printf (for the screen) instead of constantly adding new ones.
    You want to see ALL of the data, not just the tiny piece you are interested in at the moment.
    (As in... please fix that now.)
    Last edited by CommonTater; 05-01-2011 at 12:39 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating fgetc while reading a file
    By agentsmith in forum C Programming
    Replies: 1
    Last Post: 04-10-2008, 01:52 PM
  2. NOOB: Need a little help opening file and manipulating data
    By liquidcourage1 in forum C++ Programming
    Replies: 7
    Last Post: 02-26-2006, 10:27 PM
  3. How can I know the actual bytes read in a file read
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 06-08-2005, 04:23 PM
  4. Manipulating Text File?
    By Silverdream in forum C Programming
    Replies: 1
    Last Post: 03-22-2002, 06:44 PM