[Dev-C++] problem with stdin and stdout
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Luigi S. <gi...@li...> - 2002-01-03 06:36:58
|
Hello list
I tried to make a simple program that prints the amount of characters found
in a file. Here is my code:
count.c
------
#include <stdio.h>
int
main()
{
int n = 0;
while (getchar() != EOF)
n++;
printf("n = %d\n", n);
return 0;
}
---------
One could use the program in one of these 4 ways:
1) count get input from stdin and print amount of character to
stdout
2) count < foo get input from file "foo" (using redirection) print
to stdout
3) count > bar get input from stdin, print output to file "bar"
4) count < foo > bar get input from "foo", print output to "bar"
IMHO the above 4 commands are equivalent (to the extent that one
types what is contained in the file "foo", when the input comes "directly"
from stdin), so I expected the code to behave in the same way independent
of the command I used; here are my results: say that the file "foo" is the
following (an '@' stands for EOF):
foo
-----
aa@
-----
When I try command #2 and #4 I get the correct answer: that is "n = 2".
When I try command #3 (and manually type "aa+CTRL-z" on stdin) I find that
"bar" is
bar
------
n = 2
------
Strange enough when I use command #1 (and manually type "aa+CTRL-z" on
stdin) I see nothing displayed!
I have tried the above with MinGW 2.95 on a Win98SE system. I think the
problem is related to Windows, because I remember asking a similar question
to Mr. Vranos and he answered me that he had no problem, he was using MinGW
2.95 too, but his system was running a different version of Windows (I'm
not sure which one).
I would appreciate if someone, running Windows 98SE, could try the above
using my same compiler (it is the one shipped with Dev-C++ 4.0 and I think,
but am not sure, Dev-C++ 4.1) and let me know what happens: at least I
would make sure that the problem is related to my installation of Windows.
Of course any idea about what I am doing wrong is welcome.
By the way the following code works:
count2.c
------
#include <stdio.h>
int
main()
{
int n = 0;
while (getchar() != EOF)
n++;
puts(""); /* this is the "magic" line */
printf("n = %d\n", n);
return 0;
}
---------
Thank you for your time.
--
gisan
gi...@li...
|