-
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'.
-
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.
-
> You need to strip the newline from the buffer before printing it.
Which is in the FAQ.