0

This code is supposed to print all the numbers from a text file...but for some unknown reason, it is only printing the last pair.

#include<stdio.h>

int main()
{
    freopen("in.txt", "r", stdin);
    int result=0, column=0, row =0;

    while(scanf("%d %d", &row, &column) != EOF);
    {
        printf("%d %d", row, column);
    }
    return 0;
}

or

while(scanf("%d %d", &row, &column) == 2);

inside the in.txt file:
-----------------------------------------------------------
4 9
6 7
4 1

-----------------------------------------------------------

the output is
4 1

but others numbers are not showing.

but when i change the line like this,

while(scanf("%d %d", &row, &column) == EOF);
while(scanf("%d %d", &row, &column) != 2);

then it prints the first pair.
4 9

How come scanf encounter EOF at the end of the first row?

How to print all the pairs?

3
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by iamcreasy
Featured Replies
  • 1
    Salem 5,138   7 Years Ago

    > while(scanf("%d %d", &row, &column) != EOF); Guess what the ; at the end of this line does. As in [ICODE]while(scanf("%d %d", &row, &column) != EOF) { /*nothing at all here */ }[/ICODE] Read More

1

> while(scanf("%d %d", &row, &column) != EOF);
Guess what the ; at the end of this line does.

As in while(scanf("%d %d", &row, &column) != EOF) { /*nothing at all here */ }

0

Does the text file contain:

-----------------------------------------------------------

No

0

> while(scanf("%d %d", &row, &column) != EOF);
Guess what the ; at the end of this line does.

As in while(scanf("%d %d", &row, &column) != EOF) { /*nothing at all here */ }

Thanks....i cannt believe that i made this mistake. :(

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.