Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Debug Assertion Failure on gets(char) function

Reply
Thread Tools

Debug Assertion Failure on gets(char) function

 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-19-2007
Army1987 said:

>
> "Richard Heathfield" ha scritto...


<snip>

>> if(fgets(Yes_No, sizeof Yes_No, stdin) != NULL && Yes_No[0] == 'y')

>
> Is it necessary?


No, of course not. There are plenty of ways you could do this...

> I'd just use:
> int answer;
> int ch;
>
> and then:
> answer = getchar();
> do {
> ch = getchar();
> } while (ch != '\n' && ch != EOF);
> if (answer == 'y' || answer == 'Y') {


....for example, you could write five lines of code instead of one, and
still fail to deal with EOF correctly.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
 
Army1987
Guest
Posts: n/a
 
      06-19-2007

"Richard Heathfield" <> ha scritto nel messaggio
news:...
> Army1987 said:
>
>>
>> "Richard Heathfield" ha scritto...

>
> <snip>
>
>>> if(fgets(Yes_No, sizeof Yes_No, stdin) != NULL && Yes_No[0] == 'y')

>>
>> Is it necessary?

>
> No, of course not. There are plenty of ways you could do this...
>
>> I'd just use:
>> int answer;
>> int ch;
>>
>> and then:
>> answer = getchar();
>> do {
>> ch = getchar();
>> } while (ch != '\n' && ch != EOF);
>> if (answer == 'y' || answer == 'Y') {

>
> ...for example, you could write five lines of code instead of one, and
> still fail to deal with EOF correctly.


Why?
If the user sends an EOF to stdin when asked for an answer, the
program does the same as if he wrote "No!" and pressed enter, or
if he wrote "0" and pressed enter.
(Note that your program prints "Couldn't read your answer.\n" to
stderr in any of these cases, or even if the user writes "Yes.".
And it even returns 0. Also, it doesn't discard any character from
the (sizeof Yes_No)th onwards on the line. What happens if I write
y supercalifragilisticexpialidocious.txt, and the file
ilisticexpialidocious.txt can successfully be opened?)

If the line count means anything, then

answer = getchar(); do { ch = getchar(); } while (ch != '\n' && ch != EOF);
if (answer == 'y' || answer == 'Y') {
would be better.

(Or:
if (scanf("%c%*[^\n]%*c", &ans) > 0 && ans && strchr("Yy", ans)) {
where ans is an unsigned char, and string.h has been included... )


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      06-19-2007
Army1987 said:

<snip>

> If the line count means anything


The line count is merely a symptom. The cause is excessive complexity.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Debug Assertion Failure? Gone With Wind C++ 3 05-25-2008 10:57 AM
Assertion failure on hotshot.stats.load() Yang Python 1 10-27-2006 08:32 AM
gc assertion failure Todd Miller Python 3 10-30-2003 10:44 AM
RE: gc assertion failure Tim Peters Python 3 10-29-2003 10:27 PM
Debug assertion failure Kostatus C++ 1 08-03-2003 04:53 AM



Advertisments