gpt4 book ai didi

c - vfscanf.c 没有那个文件或目录

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:45 25 4
gpt4 key购买 nike

<分区>

从书上学习malloc(),我试了书上的例子。以下是示例代码。

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

int main(void)
{
double * ptd;
int max;
int number;
int i=0;

puts("how many double values will you enter?");
if (scanf("%d",&max)!=1)
{
puts("Input not proper.");
exit(EXIT_FAILURE);
}
ptd=(double *) malloc(max*sizeof(double));
if (ptd=NULL)
{
puts("Failed to assign memory.");
exit(EXIT_FAILURE);
}
puts("Enter values(q to exit)");
while (i<max && scanf("%lf", ptd+i)==1) i++;

printf("The following %d numbers are what you entered:\n",number=i);
for (i=0; i<number; i++)
{
printf("%7.2f ",ptd[i]);
if (i%7==6) putchar('\n');
}

if (i%7!=0) putchar('\n');
free(ptd);
puts("Bye!");
return 0;
}

然后我用 gcc 编译并运行它。起初它似乎工作正常,但是当我输入 double 值时(第 23、24 行),第 26 行的消息没有出来。于是通过gdb调试,遇到错误:

vfscanf.c: 没有那个文件或目录。

编辑 以下是 gdb 中的输入和结果。

how many double values wil you enter?
5
Enter values(q to exit)
1.2 2.3 3.4 4.5 5.6

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7a6a4cf in _IO_vfscanf_internal (s=<optimized out>,
format=<optimized out>, argptr=argptr@entry=0x7fffffffe0a8,
errp=errp@entry=0x0) at vfscanf.c:2444
2444 vfscanf.c: No such file or directory.

The possible duplicate in the question似乎没有解决我的问题。

错误是什么意思,我该如何解决?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com