Re: Debug Assertion Failure on gets(char) function



kudruu@xxxxxxxxx wrote On 06/18/07 11:42,:
Hello,
I am having a problem in Microsoft Visual C++ with some code I have
written (I am a bit of a novice so I appologize if this is a very
mundane problem).
This is a segment from one of the functions I am using. I am trying
to get a user to input whether they want an output file or not and if
they do to specify the name. Output_File is a global char.
Maybe there is a better way to do this, nonetheless, it compiles with
a LNK4075 warning.
When I run the code, however, it allows me to enter the if statement
and type my preference. Then it doesn't even advance to the next
print statement (in or outside of the if), it quits with the message:

[code]
Debug Assertion Failed!
File: fopen.c
Line: 55

Expression: *file != _T("\0')
[/code]

Here is the problem code:
char Yes_No[1];

printf("Do you wish to write an output file (y/n)? ");
if (gets(Yes_No) == "y"){
printf("\nPlease input the desired name of your file: ");
scanf("%s",&Output_File);
}


Your code contains at least five errors, possibly
more having to do with the portions you haven't shown.

1: NEVER USE gets()! NEVER, NEVER, NEVER!!! For
some of the reasons, see Question 12.23 in the
comp.lang.c Frequently Asked Questions (FAQ) at
http://www.c-faq.com/. (This is probably the
cause of your linker warning.)

2: A string with N "payload" characters requires
space for N+1 characters altogether, because a
zero byte '\0' follows the last payload character.
Your Yes_No array is one character long, so it
has enough room for N==0 payload characters.

3: The == operator is not the way to compare strings
for equality of their payloads. See Question 8.2
in the FAQ.

4: The way you are using scanf() is vulnerable to the
same problem as the NEVER-to-be-used gets(). See
Question 12.20 in the FAQ.

5: If Output_File is as you say a char, then it is
too small to hold a file name; see error #2. If
it is an array of char, the & operator shouldn't
be there; see Question 6.12 in the FAQ. And if
it's a char* pointer, the & operator still doesn't
belong; see the entire Section 6 of the FAQ. One
way or another, your use of Output_File is wrong.

You say you're a novice, and there's nothing shameful
about that: Comparatively few people pop out of the womb
already knowing C. But it seems to me that you are not
just "a bit of a novice" but a "rank beginner," and you
need to spend a good deal more time with a C textbook
before you go much further. Your misunderstandings are
so fundamental at the moment that Usenet is a poor vehicle
for correcting them. It's a good medium for communicating
fine points, for airing opinions, and for invective and
flame wars, but it's not a channel that's suited to mass
transfer of basic information. Hit the books!

--
Eric.Sosman@xxxxxxx
.



Relevant Pages

  • Re: Why write putc(s.i16 & 0xff, fp);
    ... char, so anything but the 8 lower bits is automatically discarded. ... Read the answer to the question 12.42 of the FAQ. ... reads 8-bit characters"). ...
    (comp.lang.c)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... Why is using char* a bad thing and why using sprintf a bad thing to, ... can be up to MAX_PATH characters). ... LPSTR lpMsgBuf; ... MessageBox(NULL, lpMsgBuf, "GetLastError() for ...
    (microsoft.public.vc.mfc)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... This means that if you develop the bad habit of using char * (left over ... It usually takes me five minutes to create a Unicode version of any of my apps, ... BOOL and bool are different data types. ... can be up to MAX_PATH characters). ...
    (microsoft.public.vc.mfc)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.server)
  • Re: Char and Varchar
    ... If the maximum length is short (<= 10 characters), ... maximum length, I also use CHAR. ... I use VARCHAR if long and short ...
    (microsoft.public.sqlserver.programming)