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?

Recommended Answers

All 4 Replies

Member Avatar for iamthwee

Does the text file contain:

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

> 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 */ }

Does the text file contain:

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

No

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.