-2

I have the following basket.txt file

Center
defence=45
training=95

Shooter
points=34
Rebounds=7

Shooter
points=8
Rebounds=5

Forward
points=8
Rebounds=5

I want to get and display only the Shooter values. To return something like this:

Shooter
points=34
Rebounds=7

Shooter
points=8
Rebounds=5

My thought is to read the file line by line and with the usage of strstr when it finds the string shooter then to print everything above it. But with the following code

int main()
{
  static const char filename[] = "basket.txt";
  FILE *file = fopen (filename, "r");
if (file!= NULL)
{
    char line[128];
    while (fgets (line, sizeof line, file)!= NULL)
    {
        char *line1 = strstr(line,"Shooter");
        if (line1)
        {
        while (fgets (line, sizeof line, file)!= NULL)
        fputs(line,stdout);
        }
    }
fclose(file);
}
else
{
    perror(filename);
}
return 0;
}

it returns me

Shooter
points=34
Rebounds=7

Shooter
points=8
Rebounds=5

Forward
points=8
Rebounds=5

So how can I change my code to have the result which I want?

UPDATE

I changed the while loop

while (fgets (line, sizeof line, file)!= NULL)
    {
        char *line1 = strstr(line,"Shooter");
        if (line1)
        {
            fgets (line, sizeof line, file);
            while (line[0] != '\n')
            {
                fputs(line,stdout);
                fgets (line, sizeof line, file);
                break;
            }

but the results are now

points=34
points=8

It does not return me the rebounds of the Shooters.

dali1985
  • 3,263
  • 13
  • 49
  • 68
  • You're printing _every line_ after the first line containing "Shooter". Is this result really so unexpected? (Hint: when should your loop terminate?) – James M Jul 09 '13 at 11:55
  • "which is not the result I want" -- so why not do what you want? It's certainly doing exactly what you're telling it to. – Jim Balter Jul 09 '13 at 12:32
  • Yes I know. So the question is how to change my code to have the result which I want. – dali1985 Jul 09 '13 at 12:35

2 Answers2

3
if (line1)
{
    while (fgets (line, sizeof line, file)!= NULL)
    fputs(line,stdout);
}

This is wrong, since fgets() doesn't return NULL until the end-of file. You want to read until an empty line is encountered:

if (line1) {
    fgets(line, sizeof line, file);
    while (line[0] != '\n') {
        fputs(line, stdout);
        fgets(line, sizeof line, file);
    }
}
  • The OP will be unhappy if the blank line happens to actually contain blanks. – Jim Balter Jul 09 '13 at 12:33
  • @JimBalter Indeed. (But again, I didn't imply it does, why would I.) But, if you wish, it's only a matter of `strchr()` and mapping `isspace()` over the characters until the newline. –  Jul 09 '13 at 12:39
  • @H2CO3 Thanks for your help for one more time. I updated my loop with your code. I used also break because without it the program never stops. But it does not return me the rebounds. Do you have any idea of what is going wrong? – dali1985 Jul 09 '13 at 12:58
  • @dali1985 Well, I haven't tested this code, I just quickly whipped it up according to the basic logic you want to implement. Maybe run it in a debugger? –  Jul 09 '13 at 13:12
0

Your inner loops finds the first Shooter and then prints the rest of the file.

The inner loop must stop at the first blank line. Something like:

while ( line[0] != '\n' )
{
    fputs...
    if ( fgets(...) == NULL )
        break;
}

This is well behaved on EOF conditions.

Gilbert
  • 3,740
  • 17
  • 19
  • Can you please take a look in my update code? As you can see now it returns me only the points and not the rebounds. – dali1985 Jul 10 '13 at 06:46