本文介绍了程序和Watson博士因不明原因崩溃了......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序运行正常,直到返回语句,其中

崩溃,因此,Watson博士(drwtsn32.exe)也崩溃。

删除通话到fgets(),一切运行正常。我认为*

堆栈或其他东西在fgets()期间搞砸了。有人可以帮忙吗?


谢谢。


#include< stdio.h>

#include< stdlib.h>


int main(int argc,char * argv []){

char ** fbuffer =(char **)malloc(8192 * sizeof(char));

FILE * fp = fopen(" data.dat"," r");

fgets(fbuffer [count],64 ,FP); //麻烦。

printf("%s",fbuffer [count]);

system(暂停);

返回0;

}

//(data.dat包含2行短文本)

The following program runs alright until the return statement, where it
crashes, and consequently, Dr. Watson (drwtsn32.exe) also crashes.
Deleting the call to fgets() and everything runs fine. I *think* the
stack or something got mess up during the fgets(). Could anyone help?

Thanks.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
char **fbuffer=(char **)malloc(8192*sizeof(char));
FILE *fp=fopen("data.dat","r");
fgets(fbuffer[count],64,fp); //Trouble here.
printf("%s",fbuffer[count]);
system("pause");
return 0;
}
//(data.dat contains 2 short lines of text)

推荐答案



HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
(新帖子12/5)


HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays




它。]


对不起,原始计数用于循环,当我发布了这个

消息我删除了循环。所以假设计数为0.(在

原始来源计数中用于循环并从

0-1取得值)


fbuffer [count] *应该由fgets()命令初始化,因为它将
存储指向字符串的指针指向第一个指定的指针

参数。


就像我说的,程序运行正常。它显示正确的字符串。

只是没有正确返回。


此外,int main()之后的代码的第一行分配了fbuffer。


it.]

Sorry, the original count was used in a loop, and when I posted this
message I deleted the loop. So just assume count is 0. (In the
original source count was used in a loop and took on the value from
0-1)

fbuffer[count] *should* be initialized by the fgets() command, since it
stores the pointer to the string to the pointer specified in the first
argument.

Like I said, the program runs fine. It displays the right string.
Just not returning correctly.

Also, the first line of the code after int main() allocates fbuffer.




它。

抱歉,原始计数在循环中使用,当我发布这个原始源计数中使用了一个循环并从0-1取得值)

fbuffer [count] *应该*由fgets()命令初始化,因为它将指向字符串的指针存储到第一个
参数中指定的指针。

就像我说的那样,程序运行精细。它显示正确的字符串。
只是没有正确返回。

此外,int main()之后的代码的第一行分配fbuffer。



it.]

Sorry, the original count was used in a loop, and when I posted this
message I deleted the loop. So just assume count is 0. (In the
original source count was used in a loop and took on the value from
0-1)

fbuffer[count] *should* be initialized by the fgets() command, since it
stores the pointer to the string to the pointer specified in the first
argument.

Like I said, the program runs fine. It displays the right string.
Just not returning correctly.

Also, the first line of the code after int main() allocates fbuffer.



它为fbuffer分配内存。它确实*不*将内存分配给* fbuffer中的

指针*(这是一个指针指向char的指针)。你需要

来为每一行分配内存。


HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀
(新帖子12/5)


It allocates memory to fbuffer. It does *not* allocate memory to the
pointers *in* fbuffer (which is a pointer-to-pointer-of-char). You need
to allocate memory for each line.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


这篇关于程序和Watson博士因不明原因崩溃了......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 00:17