-2

I wanted to learn how fgets works. For this I wrote code to print "hi" before every line, which is going to be printed from an another text file named input.txt using fgets.

But it is showing 2 his between consecutive lines instead of 1. I can't understand why?

My code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char singleline[150];//storing each line of input.txt as singleline arrays.
    FILE *fp;//file pointer named fp.
    fp=fopen("input.txt","r");//reading the text file input.txt
    while (!feof(fp)){
        fgets(singleline,150,fp);
        printf("hi\n");// to check that before each line hi is printed?
        puts(singleline);
    }

    return 0;
}

My text file

rohit sharma,batsman,1,35.0,40.0,2200,20,95  
viru,batsman,2,28.0,45.0,1800,02,75   
suresh raina,batsman,3,38.0,35.0,2300,15,98  
suryaky,batsman,4,30.0,0.0,500,0,10   
abd,batsman,5,37.2,0.0,1200,0,50
dhoni,batsman,6,45.2,0.0,2100,0,85  
albie,allrounder,7,24.87,27.65,945,80,86  
ashwin,bowler,8,8.82,24.37,150,82,85  
naraine,bowler,9,6.67,16.94,40,67,49  
johnson,bowler,10,12.25,21.33,98,45,33  
starc,bowler,11,14.17,28.71,85,14,14

Output

hi   
rohit sharma,batsman,1,35.0,40.0,2200,20,95   

hi
hi  
viru,batsman,2,28.0,45.0,1800,02,75   
(till the end)  
johnson,bowler,10,12.25,21.33,98,45,33  

hi 
hi  
starc,bowler,11,14.17,28.71,85,14,14

What is the error here?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Sunil Kumar
  • 390
  • 1
  • 7
  • 25
  • 3
    Is that the exact output? Also [`while (!feof(fp))` is always wrong](http://stackoverflow.com/a/26557243/1983495). Also, you don't check if `fopen()` succeeded which obviously didn't happen since there is something being read, but it's a bad idea to ignore that, apart from that I am unable to believe that given your input file, the output is the one you posted. – Iharob Al Asimi Apr 18 '15 at 14:03
  • 1
    I've just checked - `fgets()` works just fine – qrdl Apr 18 '15 at 14:49

1 Answers1

2

What you posted gives:

hi
rohit sharma,batsman,1,35.0,40.0,2200,20,95  

hi
viru,batsman,2,28.0,45.0,1800,02,75  

as expected, since you do not eat the trailing newline. You can do it by adding this line after reading into singleline.

if(strlen(singleline) != 0) // because you may have an empty file
  singleline[strlen(singleline) - 1] = '\0';

You could also do it like alk suggested:

singleline[strcspn(singleline, "\n")] = 0;

taken from this answer.

Also feof() in the control statement of the while loop makes me redirect you to this question: Why is while ( !feof (file) ) always wrong?

Thanks alk and iharob for the useful comments.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • This will not work because strlen itself relies on the null terminator! – Marco Apr 18 '15 at 14:09
  • Did you downvote @d3l? `fgets()` will put a null terminator and this works. – gsamaras Apr 18 '15 at 14:10
  • 1
    @d3l "*strlen itself relies on the null terminator*" this correct, but there **is** a `0`-terminator. – alk Apr 18 '15 at 14:11
  • @alk with a 2nd glance at the code I will agree with you. However, this is the output I got..I will check! – gsamaras Apr 18 '15 at 14:12
  • 3
    `strlen() - 1` is dangerous as long one cannot make sure the parameter passed to `strlen()` does not refer to a `0`-length "string". – alk Apr 18 '15 at 14:13
  • @G.Samaras I always find that the safest thing to do with `strlen()` is to store it's value. And then use it correctly by checking what that value is. – Iharob Al Asimi Apr 18 '15 at 14:17
  • @alk, it is because of `puts`. Writes the C string pointed by str to the standard output (stdout) and appends a newline character ('\n'). iharod agreed, – gsamaras Apr 18 '15 at 14:17
  • 1
    Oh yes, its `puts()` not `fputs()`, my bad. The latter does not append a new-line. – alk Apr 18 '15 at 14:19
  • @alk that made me refresh my memory about `puts()`, so no bad at all, thanks! ;) – gsamaras Apr 18 '15 at 14:21
  • 1
    A save and very elegant way to cut off any kind of traling new-line is here: http://stackoverflow.com/a/28462221/694576 – alk Apr 18 '15 at 14:22