-1
int k;
char string[100];
    for(k = 0; k < MAX_STATIONS; k++){
        fgets(string,100,fp);
        printf("%s", string);
    }

So I've got my sniped of code here, and when I try to print the text from the file, the last line is either repeated maybe 10 times.

195     11.00  2013 11 02 15 32   49.76668  234.97289    30
196     11.40  2013 11 02 15 32   49.70103  235.04120    30
197      7.30  2013 11 02 15 32   48.52185  236.57961     5
199     10.00  2013 11 02 15 32   48.39511  236.69492     5
200      8.50  2013 11 02 15 32   48.48800  236.67270     5  200      8.50  2013 11 02     15 32   48.48800  236.67270     5  200      8.50  2013 11 02 15 32   48.48800  236.67270     5  200      8.50  2013 11 02 15 32   48.48800  236.67270     5  200      8.50  2013 11 02 15 32   48.48800  236.67270     5

That is my output, any help would be much appreciated. Also, I'd like a bit of clarification, what do we do with the middle part of fgets(string,100,fp) (100 in this case).

Thanks

EDIT: I seem to have solved the problem by adding

if(feof(fp)) break;

Is that a valid solution?

EDIT2: I have also solved it by putting a while loop to check if it has reached the end of the stream within the forloop:

int k;
char string[100];

for(k = 0; k < MAX_STATIONS; k++){
    while(!feof(fp)){
        fgets(string,100,fp);
        printf("%s", string);
        //if(feof(fp)) break;

    }
}
Jake Cooper
  • 109
  • 1
  • 3
  • 9
  • 1
    You sniped the code? But you didn't snipe the debugger... – Kerrek SB Nov 11 '13 at 00:29
  • @KerrekSB [Neither did he use the site search](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong)... –  Nov 11 '13 at 00:38

2 Answers2

1

The correct way to solve it is to test the input operation; using feof() is possible but not necessary, and requires care to get it right. The way you show of using feof() is wrong on two counts; you don't test the I/O operation, and you introduce a loop that eats your entire input for k == 0 leaving nothing for subsequent rows.

int k;
char string[100];
for (k = 0; k < MAX_STATIONS; k++)
{
    if (fgets(string, sizeof(string), fp) == 0)
        break;
    printf("%d: %s", k, string);
}

Or:

int k;
char string[100];
for (k = 0; k < MAX_STATIONS && fgets(string, sizeof(string), fp) == 0; k++)
    printf("%d: %s", k, string);

Note that fgets() retains the newline (unless the input line is too long to fit).

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Does the method I have outlined work? Also, I'm wondering if you could outline what the 100 in fgets(string, 100, fp) does? I thought it was "read x amount of characters" but if I set it to 10, it will still read over 60 characters from my file. EDIT: Read your link, quite helpful.So you're saying the way i've done it is good, but not optimal? – Jake Cooper Nov 11 '13 at 00:37
  • No; your alternative doesn't work — see my comments in the updated answer written while you were typing your comment/question. The 100 is the size of the string; it tells `fgets()` how much space there is in the string to store data. – Jonathan Leffler Nov 11 '13 at 00:39
  • Ahh okay, reading your link I understand what youre saying. If 100 is the size of the string, then how come if I set it to 10, it can still read over 60 chars? – Jake Cooper Nov 11 '13 at 00:44
  • 1
    It reads 10 characters at a time, but your printing prints the 10 characters at a time without any punctuation added. Note how I added `%d:` to the format (and `k` to the arguments). Try that with your `10` code and see `0:first ten 1:characters2: in the in3:put...` (if I got my counting correct). – Jonathan Leffler Nov 11 '13 at 00:47
0

Perhaps it might be wise to read the manual page http://www.cplusplus.com/reference/cstdio/fgets/

If does return a value that may suggest that the file has ran out

Ed Heal
  • 59,252
  • 17
  • 87
  • 127