Skip to main content
GameDev.net gamedev.net
🔒 Locked

stdio and getc ()

Started by Densun Nov 17, 2000 at 9:54 PM 4 replies 650+ views
Original Post
Densun
Densun
Hmmm...I don't know how to tab the code here. I'm reading through "The C Programming Language" right now, and one of their examples for getting input does not seem to work like it's supposed to for me. Here's theire code: #include int main () { ...long nc; ...nc = 0; ...while (getchar () != EOF) ......++nc; ...printf ("%ld\n", nc); ...return 1; } This will never end, so I need to end the program through Windows. I don't see how I can get the keyboard to return EOF. --- my website Edited by - Densun on 11/17/00 9:56:15 PM Edited by - Densun on 11/17/00 9:57:08 PM Edited by - Densun on 11/17/00 9:57:52 PM
jaxson
jaxson
I think a CTRL-Z will work in DOS and something like a CTRL-D works in UNIX as the end of file character. At least I think those are the right letters
Densun
Densun
Ctrl+Z ends the program, but it quits before processing the rest of the lines before the return statement. Like in the example I gave, it quits before displaying the number of characters counted.

---
my website
jaxson
jaxson
quote:
Original post by Densun

Ctrl+Z ends the program, but it quits before processing the rest of the lines before the return statement. Like in the example I gave, it quits before displaying the number of characters counted.



Hmm... Is the window just closing too fast for you to read the number? If so, try putting in a delay after the printf. If you are just getting a DOS prompt with no output, then I''m not sure what the problem would be. Is the code from your first post exactly what you''re compiling?
Densun
Densun
The DOS window does not close, it just says "Press any key to continure..." The above code is all there is, except the stdio header did not show up because it was betweent the > and <. One way around it is if you have it quit on a certain letter like q.

---
my website

Edited by - Densun on November 20, 2000 9:39:04 AM
bishop_pass
bishop_pass
#define EOF (-1)

That is the definition of EOF. getchar() returns an int. So, tell me, can you produce a negative int with the keyboard? What I think happens is the first 8 bits of the int get filled with the character code. That leaves the int positive.

Generally, you would check for the RETURN key code. getchar () returns an int but leaves the first 8 bits as useful data. This way, we can read ANY byte combo from a file without it being confused as an EOF. Afterall, a file should be able to contain any byte.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.