问题描述
这code,当我尝试使用VC2010调试失败:
This code fails when I try to debug it using VC2010:
char frd[32]="word-list.txt";
FILE *rd=fopen(frd,"r");
if(rd==NULL)
{
std::cout<<"Coudn't open file\t"<<frd;
exit(1);
}
char readLine[100];
while(fgets(readLine, 100, rd) != NULL)
{
readLine[strlen(readLine) - 1] = '\0';
char *token = NULL;
token = strtok(readLine, " ,");
insert(readLine);
}
调试结果
---------------------------微软的Visual C ++调试库-----------
调试断言失败!
程序:... \\文档\\ Visual Studio 2010的\\项目\\博鳌亚洲论坛\\调试\\ bfa.exe
文件:F:\\ DD \\ vctools \\ crt_bld \\ self_x86 \\ CRT \\ SRC \\ fgets.c行:57
Program: ...\documents\visual studio 2010\Projects\bfa\Debug\bfa.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\fgets.c Line: 57
防爆pression:(!STR = NULL)
Expression: ( str != NULL )
有关您的程序如何可以导致了断言失败的信息,
请参阅Visual C ++文档上断言。
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(preSS重试来调试应用程序)
(Press Retry to debug the application)
我得到的错误号为2;
The errno I get is 2;
推荐答案
我的猜测是,该文件无法打开,而你还在它传递给与fgets。你如果(RD == NULL),如果是空的,它只是打印出一条消息,并继续执行不停止与fgets的执行。
My guess is that the file is failing to open, and you're still passing it to fgets. Your if(rd==NULL) doesn't stop execution of the fgets if it's null, it just prints out a message and continues with execution.
一些非常基本的errorr处理:
Some very basic errorr handling:
const char* frd = "word-list.txt";
FILE *rd=fopen(frd,"r");
if(rd==NULL) {
std::cout<<"Coudn't open file"<<endl;
return 1;
}
char readLine[100];
while(fgets(readLine, 100, rd) != NULL)
{
readLine[strlen(readLine) - 1] = '\0';
char *token = NULL;
token = strtok(readLine, " ,");
insert(readLine);
}
这篇关于为什么我得到一个断言失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!