fgets question

This is a discussion on fgets question within the C Programming forums, part of the General Programming Boards category; my fgets is scanning a new line it seems. is there a way to avoid that? Here is my code ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    4

    fgets question

    my fgets is scanning a new line it seems. is there a way to avoid that?

    Here is my code if you are interested:

    Code:
    if(infile == NULL){
        printf("File failure.\n");
        exit(1);
    }
    lines=0;
    while(fgets(text, 81, infile) != NULL){
      lines++;
      if (lines%2 != 0){
        printf("Student %s: ", text);
      }else {
        sscanf(text, "%d %d %d %d %d",
           score, score+1, score+2, score+3, score+4);
        for(k=0, average=0.0; k<5; k++)
           average += score[k];
        average /= 5.0;
      if (average == 100)                    {ch_grade = 'A'; ch_mod = '+';}
      else if(average >= 90 && average < 100){ch_grade = 'A'; ch_mod = ' ';}
      else if(average >= 80 && average < 90) {ch_grade = 'B'; ch_mod = ' ';}
      else if(average >= 65 && average < 80) {ch_grade = 'C'; ch_mod = ' ';}
      else if(average >= 50 && average < 65) {ch_grade = 'D'; ch_mod = ' ';}
      else if(average < 50)                  {ch_grade = 'F'; ch_mod = ' ';}
      printf("Average %5.1f; Grade = %c%c.\n",
                    average, ch_grade, ch_mod);
    I'm printing only twice and want the template
    Student 'Name': Average 'Score'; Grade 'Letter''Modifier'.

    But am getting:
    Student 'Name'
    : Average 'Score'; Grade 'Letter''Modifier'.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    fgets only reads one line, if there is enough room in the buffer.
    But fgets also reads the newline, so that is probably causing the output you get. You need to strip the newline from the buffer before printing it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > You need to strip the newline from the buffer before printing it.
    Which is in the FAQ.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question on using fgets
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 10-17-2008, 10:23 AM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. fgets problem (i think)
    By GanglyLamb in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 10:19 AM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21