Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 22nd, 2005, 8:48 AM   #1
o0zi
Newbie
 
Join Date: May 2005
Posts: 1
Rep Power: 0 o0zi is on a distinguished road
Resetting stdin after EOF

I have a piece of code that looks like this:

---example.c---

#include <stdio.h>

int main() {
        char c;
        while ((c = getchar()) && (c != EOF))
                ;
        putchar('\n');
        c = getchar();
        printf("%c\n", c);
}
Entering input from the keyboard, this program works fine.
However, if I then redirect input to this program (e.g. dmesg | ./example), then I get a garbage character printed to the console, and the program does not wait for input from the keyboard.
I assume this is something to do with resetting stdin - how can I pass input back to the keyboard when stdin has been set to receive input from a redirect?

Thanks,
Rob
o0zi is offline   Reply With Quote
Old May 22nd, 2005, 9:21 AM   #2
Nuticulus
Programmer
 
Nuticulus's Avatar
 
Join Date: May 2005
Location: England
Posts: 61
Rep Power: 4 Nuticulus is on a distinguished road
If you're piping stdout of dmesg to the stdin of ./example, can you provide stdin from the keyboard also? Just wondering.

Edit: thinking about it, if you've reached EOF, getchar() will return EOF instantly, because there's absolutely nothing left to be read. And you can't just magically give it input from stdin, because that comes from the program piping input, not your keyboard.

I'm puzzled as to why you made this program. Why not just pipe output to /dev/null?

Last edited by Nuticulus; May 22nd, 2005 at 9:38 AM.
Nuticulus is offline   Reply With Quote
Old May 23rd, 2005, 11:25 PM   #3
osvaldomarques
Newbie
 
Join Date: May 2005
Location: Rio de Janeiro - Brazil
Posts: 2
Rep Power: 0 osvaldomarques is on a distinguished road
Hi Rob,

In that example, you have a semicolon just after the while expression. It ends the while block; so this program reads all the contents of the input and discards it until you get an eof; then it prints an "new line", reads the input again, probably eof also, and prints it.

When you are typing you see the characters because the console is in cannonical mode, echoing them as it passes to the program. When you pipe another content to the standard input of your program, you don't see the characters because there is no echo as they doesn't come from the terminal.

So, you will see just the garbage at the end of the file. For this program do what I suppose you intend to, we would to change it to
#include <stdio.h>

int main() {
        char c;
        while ((c = getchar()) && (c != EOF))
                printf("%c\n", c);
        putchar('\n');
}

The colateral effect is when typing you will see 2 characters for each typing; one from echo and one from printf(). Then you would need to program the console to do not echo the char when the standard input is from the console.

Regards,

Osvaldo.
osvaldomarques is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:31 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC