From: Poster Matt on
Hi,

My code allows a user to redirect a file to stdin so that some text data can be
read, no problem.

Later a user confirmation to proceed is requested, using getchar(). This is not
working because of what's left in stdin.

Initially I was actually stopping reading stdin when some specific data being
searched for was located, ignoring what was left in stdin. When I realized what
was happening, I made sure I continued reading stdin all the way through.

The problem seems to be that my user confirmation call to getchar() receives the
following char: '�'. I looked in the ASCII table and it is the final char, 255
or FF. But I have no idea what this is or why it's there.

I've tried reading stdin through to the end with fgets...

while (fgets(buffer, maxLineLen, stdin) != NULL)
check line for what I want;

....and also with scanf (just reading %s):

while (scanf("%s", buffer) != EOF)
check line for what I want;

but I always the get the '�' char when it comes to my user confirmation.

The faq suggests trying the following code:

while((c = getchar()) != '\n' && c != EOF)
/* discard */ ;

No luck, I still get '�'.

I even tried calling fgets and scanf immediately before calling getchar and
printfing what was got - but there is nothing to printf - and yet '�' is still
there to sabotage the call to getchar().

Even though the faq warns against it, I even tried fflush(stdin). Nope. :(

What is this little fellow, '�'. Why's it there? How can I get rid of it?

I should mention that if I do not redirect a file to stdin on the command line,
the user confirmation works fine, no sign at all of '�'.

Many thanks.
From: Lew Pitcher on
On February 28, 2010 16:56, in comp.unix.programmer,
postermatt(a)no_spam_for_me.org wrote:

> Hi,
>
> My code allows a user to redirect a file to stdin so that some text data
> can be read, no problem.
>
> Later a user confirmation to proceed is requested, using getchar(). This
> is not working because of what's left in stdin.
>
> Initially I was actually stopping reading stdin when some specific data
> being searched for was located, ignoring what was left in stdin. When I
> realized what was happening, I made sure I continued reading stdin all the
> way through.
>
> The problem seems to be that my user confirmation call to getchar()
> receives the following char: 'ÿ'. I looked in the ASCII table and it is
> the final char, 255 or FF.

Actually, it's not ASCII. ASCII only extends from code points 0x00 to 0x7f;
anything past that isn't ASCII.

> But I have no idea what this is or why it's
> there.
>
> I've tried reading stdin through to the end with fgets...
>
> while (fgets(buffer, maxLineLen, stdin) != NULL)
> check line for what I want;
>
> ...and also with scanf (just reading %s):
>
> while (scanf("%s", buffer) != EOF)
> check line for what I want;
>
> but I always the get the 'ÿ' char when it comes to my user confirmation.
>
> The faq suggests trying the following code:
>
> while((c = getchar()) != '\n' && c != EOF)
> /* discard */ ;
>
> No luck, I still get 'ÿ'.
>
> I even tried calling fgets and scanf immediately before calling getchar
> and printfing what was got - but there is nothing to printf - and yet 'ÿ'
> is still there to sabotage the call to getchar().
>
> Even though the faq warns against it, I even tried fflush(stdin). Nope. :(
>
> What is this little fellow, 'ÿ'. Why's it there? How can I get rid of it?
>
> I should mention that if I do not redirect a file to stdin on the command
> line, the user confirmation works fine, no sign at all of 'ÿ'.
>
> Many thanks.

Show us the code you use.

I'm betting that you declared your variable as a char rather than an int,
and are receiving a (truncated) EOF. But only your code will tell.

--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
---------- Slackware - Because I know what I'm doing. ------


From: Nicolas George on
Poster Matt wrote in message
<%DBin.45925$Ym4.25977(a)text.news.virginmedia.com>:
> The problem seems to be that my user confirmation call to getchar()
> receives the following char: '�'.

My crystal ball shows me that you are storing the result of getchar in a
variable declared as char or unsigned char. You should store it in an int.
From: Eric Sosman on
On 2/28/2010 4:56 PM, Poster Matt wrote:
> Hi,
>
> My code allows a user to redirect a file to stdin so that some text data
> can be read, no problem.

What do you mean by this? That your program uses freopen()
to change where stdin gets input, or that your program just reads
from stdin (which the user may have redirected via a shell)?

> Later a user confirmation to proceed is requested, using getchar(). This
> is not working because of what's left in stdin.

How does the user enter a confirmation? By pressing keys on
the keyboard, perhaps? How does the user enter a confirmation if
stdin is reading from a file and paying no attention at all to
the keyboard, hmmm?

> Initially I was actually stopping reading stdin when some specific data
> being searched for was located, ignoring what was left in stdin. When I
> realized what was happening, I made sure I continued reading stdin all
> the way through.
>
> The problem seems to be that my user confirmation call to getchar()
> receives the following char: '�'. I looked in the ASCII table and it is
> the final char, 255 or FF. But I have no idea what this is or why it's
> there.

Hint: Print your system's value for the macro EOF. (Besides:
As discussed above, nothing the user does at the keyboard will
make any difference at all if stdin isn't looking at the keyboard.)

>[... remainder snipped ...]

--
Eric Sosman
esosman(a)ieee-dot-org.invalid
From: Poster Matt on
Lew Pitcher wrote:
>
> Show us the code you use.
>
> I'm betting that you declared your variable as a char rather than an int,
> and are receiving a (truncated) EOF. But only your code will tell.

Nicolas George wrote:
>
> My crystal ball shows me that you are storing the result of getchar in a
> variable declared as char or unsigned char. You should store it in an int.

I was using a char but was casting the getchar() to a char. I've now changed it
to an int, but the same problem persists. :(

The code is on pastie or below: http://pastie.org/847332

Thanks guys. (It's late in my TZ so bed time, will reply tomorrow).

By the way I've tried removing the isatty call and that makes no difference.


int ExamineStdinForUrl(void)
{
// Make sure the user redirected a file to stdin.
// If a file was redirected on the command line then the isatty() call
// will return 0, but if not stdin will be the terminal IE. keyboard.
// Don't read stdin from the terminal just from a redirected file.
if (isatty(fileno(stdin)) != 0)
return false;

const int maxLineLen = 1024;
char buffer[maxLineLen];

// Read the contents of the redirected file looking for an URL.

int found = false;

// Both while's result in the same thing.
while (fgets(buffer, maxLineLen, stdin) != NULL)
//while (scanf("%s", buffer) != EOF)
{
// If buffer looks like an URL (starts with 'http'), store it.
// Only interested in the first URL in the file, ignore others.
if (found == false)
{
if (CheckForUrl(buffer) == true)
found = true;
}
}

return found;
}

int GetUserConfirmation(void)
{
printf("\nAre all the settings and options correct?\n" \
"Do you want to proceed? (Y = Yes, N = No): ");

// Was using:
//char c = (char) getchar();

// Now using - has the same effect.
int c = getchar();

if ( (c == 'y') || (c == 'Y') )
return true;

// This printf outputs: 'ÿ'
printf("'%c'\n", c);

return false;
}