473,973 Members | 41,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Debug Assertion Failure on gets(char) function

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:

Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4.  
  5. Expression: *file != _T("\0')
  6.  
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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}

Jun 18 '07 #1
22 3110
On Jun 18, 9:42 am, kud...@gmail.co m wrote:
Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4. Expression: *file != _T("\0')
  5.  

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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}
So I did a little bit of tweaking and now it reads:
printf("Do you wish to write an output file (y/n)? ");
scanf("%s",&Yes _No);
printf("Got Here\n");
if (Yes_No[0] == 'y'){
printf("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}

This works only for the case of y, however I still get the error on n
(and any other letter). After this function, there are several other
functions that must be called using Output_File, is it possible to set
a character array to Null or False?

Jun 18 '07 #2
On Jun 18, 9:42 am, kud...@gmail.co m wrote:
Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4. Expression: *file != _T("\0')
  5.  

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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}
So I did a little bit of tweaking and now it reads:
printf("Do you wish to write an output file (y/n)? ");
scanf("%s",&Yes _No);
printf("Got Here\n");
if (Yes_No[0] == 'y'){
printf("\nPleas e input the desired name of your file:
");
scanf("%s",&Out put_File);
}

This works only for the case of y, however I still get the error on n
(and any other letter).

Putting an else statement in that reads:
else
ACS_Output_File = NULL;
Only produces an error that my left operand must be an l-value (type
mismatch). I thought the syntax
else
ACS_Output_File[] = NULL;
would work but it gives me a bracket error.

After this function, there are several other
functions that must be called using Output_File, is it possible to
set
a character array to Null or False?

Jun 18 '07 #3
ku****@gmail.co m said:

<snip>
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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}
Never use gets. Never use %s in scanf. Never compare strings with ==.
Never use a single-character array to store a non-empty string.

#include <stdio.h>
#include <string.h>

int chomp(char *s)
{
char *p = strchr(s, '\n');
if(p != NULL)
{
*p = '\0';
}
return p != NULL;
}

int main(void)
{
char Yes_No[16] = {0};
char Filename[FILENAME_MAX] = {0};

printf("Do you wish to write an output file (y/n"? ");
fflush(stdout);
if(fgets(Yes_No , sizeof Yes_No, stdin) != NULL)
{
printf("Please input the desired name of your file: ");
fflush(stdout);
if(fgets(Filena me, sizeof Filename, stdin) != NULL)
{
if(chomp(Filena me))
{
/* Now:
open your file
if it opened okay
write stuff
check that it wrote okay
close it
*/
}
else
{
fprintf(stderr, "Filename too long.\n");
}
}
else
{
fprintf(stderr, "Couldn't read filename.\n");
}
}
else
{
fprintf(stderr, "Couldn't read your answer.\n");
}
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 18 '07 #4
Typo corrections. See below.

Richard Heathfield said:
ku****@gmail.co m said:

<snip>
>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("\nPlea se input the desired name of your file: ");
scanf("%s",&Ou tput_File);
}

Never use gets. Never use %s in scanf. Never compare strings with ==.
Never use a single-character array to store a non-empty string.

#include <stdio.h>
#include <string.h>

int chomp(char *s)
{
char *p = strchr(s, '\n');
if(p != NULL)
{
*p = '\0';
}
return p != NULL;
}

int main(void)
{
char Yes_No[16] = {0};
char Filename[FILENAME_MAX] = {0};

printf("Do you wish to write an output file (y/n"? ");
Spurious " there. Should read:

printf("Do you wish to write an output file (y/n)? ");
fflush(stdout);
if(fgets(Yes_No , sizeof Yes_No, stdin) != NULL)
Sheesh. I forgot to check that the answer was yes.

Replace that line with:

if(fgets(Yes_No , sizeof Yes_No, stdin) != NULL && Yes_No[0] == 'y')
{
printf("Please input the desired name of your file: ");
fflush(stdout);
if(fgets(Filena me, sizeof Filename, stdin) != NULL)
{
if(chomp(Filena me))
{
/* Now:
open your file
if it opened okay
write stuff
check that it wrote okay
close it
*/
}
else
{
fprintf(stderr, "Filename too long.\n");
}
}
else
{
fprintf(stderr, "Couldn't read filename.\n");
}
}
else
{
fprintf(stderr, "Couldn't read your answer.\n");
}
return 0;
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 18 '07 #5
ku****@gmail.co m 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:

Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4. Expression: *file != _T("\0')
  5.  

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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_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 misunderstandin gs 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!

--
Er*********@sun .com
Jun 18 '07 #6
In article <rP************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>Never compare strings with ==.
Hardly ever.

It is sometimes useful to "intern" strings, that is, ensure that there
is always only one string with a given value in a given set. The main
reason for doing this is exactly so that you can compare them with ==.

I once had a program that went usefully faster after "strcmp(a,b ) == 0"
was replaced with "a == b || strcmp(a, b) == 0", because there was a very
high chance that equal strings would have come from the same source and
thus be equal as pointers.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 18 '07 #7
ku****@gmail.co m wrote:
>
Hello,
I am having a problem in Microsoft Visual C++ with some code I have
[...]
Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4. Expression: *file != _T("\0')
  5.  
You would have to ask in a Microsoft-specific group about the
exact meaning of that asssertion error.

[OT response, using magical knowledge]

The error means _tfsopen() was passed a filename of "".
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("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
}
I find it unlikely that this is the code where the failure takes
place, given that I don't see any file opens here, though it is
possible. (That's where an MS-specific group comes in handy.)

However, getting back to non-platform-specific things, what happens
if the user enters more than zero characters at the "gets(Yes_N o)"
call, given the definition of "char Yes_No[1]"?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 18 '07 #8
Thank you very much for your input. I changed the compare convention
to use string compare as the extra clock cycles will not matter in
this instance. However, I found that my problem actually stemmed from
passing a global variable around do different functions. Instead, I
set a file write variable as a boolean and passed it around to my
functions. The file string name is then used in two separate
functions.
Sorry for not specifying my file string name to begin with, it was an
array of 80 characters (long enough for a file name). The part of the
code that contains the scanf functions is part of a function
containing a case statement where I listen for event messages over a
com port and must write each separate one to a file (previously I had
not put in a file write option).
Apologies all around to the C community for using gets.
Thanks for all the help!

Jun 18 '07 #9
On Mon, 18 Jun 2007 08:42:42 -0700, ku****@gmail.co m wrote:
>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:

Expand|Select|Wrap|Line Numbers
  1. Debug Assertion Failed!
  2. File: fopen.c
  3. Line: 55
  4. Expression: *file != _T("\0')

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"){
At this point, your code invokes undefined behavior. Unless the user
does nothing but press the enter key, the input will involve at least
two characters. Your array has room for only one. You overflow your
buffer. Your solution to this problem should involve using a function
other than gets.

After you solve that problem, look in your reference book for how to
compare strings. The == operator is not what you want.
> printf("\nPleas e input the desired name of your file: ");
scanf("%s",&Out put_File);
You didn't show the definition of Output_File but 99+% of the time
this is wrong. What type of argument must the %s conversion
correspond to? What type is your argument?
> }

Remove del for email
Jun 18 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5512
by: Sathyaish | last post by:
I noticed that gets() reads into the buffer even if the you've not allocated enough memory. For instance, if you do: char *str=(char*)malloc(sizeof(char)); printf("Enter something about yourself below:\n\n"); gets(str); printf("\n\n"); puts(str);
10
6264
by: paytam | last post by:
hi all can you tell me what's the wrong with this code? I use gcc compiler,but when I wanted to use gets() function in my code but it takes a dangerous warning(the gets function is dangerous and should not be used),I don't know why.Any way,I decided to write my own gets()
89
6130
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
0
10347
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11810
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11399
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10901
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
10070
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6409
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
5146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4726
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3755
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.