Re: Debug Assertion Failure on gets(char) function
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Mon, 18 Jun 2007 12:15:44 -0400
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
.
- References:
- Debug Assertion Failure on gets(char) function
- From: kudruu
- Debug Assertion Failure on gets(char) function
- Prev by Date: Re: How to find free disk space using C
- Next by Date: Re: Debug Assertion Failure on gets(char) function
- Previous by thread: Re: Debug Assertion Failure on gets(char) function
- Next by thread: Re: Debug Assertion Failure on gets(char) function
- Index(es):
Relevant Pages
|