-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.

3
  • 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 9, 2013 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 9, 2013 at 12:32
  • Yes I know. So the question is how to change my code to have the result which I want.
    – dali1985
    Jul 9, 2013 at 12:35

2 Answers 2

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);
    }
}
4
  • The OP will be unhappy if the blank line happens to actually contain blanks.
    – Jim Balter
    Jul 9, 2013 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.
    – user529758
    Jul 9, 2013 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 9, 2013 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?
    – user529758
    Jul 9, 2013 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.

1
  • 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, 2013 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.