在gets(char)函数上调试断言失败 [英] Debug Assertion Failure on gets(char) function

查看:87
本文介绍了在gets(char)函数上调试断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我在Microsoft Visual C ++中遇到了一些问题我有一些代码我写了(我是一个新手所以我道歉,如果这是这是一个很平常的问题。

这是我正在使用的其中一个功能的片段。我正在尝试

让用户输入他们是否想要输出文件,如果他们确实要指定名称。 Output_File是一个全局字符。

也许有更好的方法可以做到这一点,但是,它编译了

a LNK4075警告。

当我运行代码,但是,它允许我输入if语句

并输入我的首选项。然后它甚至没有前进到下一个

打印语句(在if之内或之外),它退出并显示以下消息:

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:

展开 | 选择 | Wrap | 行号

推荐答案

6月18日上午9:42,kud ... @ gmail.com写道:
On Jun 18, 9:42 am, kud...@gmail.com wrote:
展开 | 选择 | Wrap | 行号


6月18日,9:42 am,kud ... @ gmail.com写道:
On Jun 18, 9:42 am, kud...@gmail.com wrote:
展开 | 选择 | Wrap | 行号


ku **** @ gmail.com 说:


< snip>
ku****@gmail.com said:

<snip>

这是问题co de:

char Yes_No [1];


printf(&你想写一个输出文件(是/否)? ");

if(gets(Yes_No)==" y"){

printf(" \\\
Please输入所需的文件名:" ;);

scanf("%s",& Output_File);

}
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);
}



从不用得到。切勿在scanf中使用%s。切勿将字符串与==进行比较。

切勿使用单字符数组存储非空字符串。


#include< stdio.h>

#include< string.h>


int chomp(char * s)

{

char * p = strchr(s,''\ n'');

if(p!= NULL)

{

* p =''\''';

}

返回p!= NULL;

}


int main(无效)

{

char Yes_No [16] = {0};

char文件名[FILENAME_MAX ] = {0};


printf(&你想写一个输出文件(y / n?");

fflush( stdout);

if(fgets(Yes_No,sizeof Yes_No,stdin)!= NULL)

{

printf(" Please input the所需的文件名:");

fflush(stdout);

if(fgets(Filename,sizeof Filename,stdin)!= NULL)

{

if(chomp(Filename))

{

/ *现在:

打开你的档案

如果它打开好吧

写东西

检查它写的没关系

关闭它

* /

}

其他

{

fprintf(stderr,文件名太长了。 \ n;

}

}

其他

{

fprintf(stderr,无法读取filename.\ n);

}

}

其他

{

fprintf(stderr,无法阅读你的答案。\ n);

}

返回0;

}


-

Richard Heathfield

Usenet是一个奇怪的放置" - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

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(Filename, sizeof Filename, stdin) != NULL)
{
if(chomp(Filename))
{
/* 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.


这篇关于在gets(char)函数上调试断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆