gpt4 book ai didi

c - 害怕。寻找。告诉。 "warning: value computed is not used "

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:48 24 4
gpt4 key购买 nike

如何代替

char text[10000]; 

char text[fp_len];

我也收到这个警告:

warning: value computed is not used

FILE *fp;
fp = fopen(path, "r");
int fp_len;
fseek(fp, 0, SEEK_END)+1;
fp_len = ftell(fp);
fseek(fp, 0, SEEK_SET);
printf("%d---", fp_len);

char text[10000];
fread(text, fp_len, 1, fp);

printf("%s", text);

我还应该添加:

text[]= '\0'

到底部。 (在 printf 之上)

最佳答案

您会收到未使用值的警告,因为当您向返回值添加 +1 时,您什么也没做

fseek(fp, 0, SEEK_END)+1;

被评估为

fseek(fp, 0, SEEK_END);

但是编译器会警告你,也许你忘了在某处分配结果。


要读取数据,请使用动态分配在 HEAP 区域中保留内存,否则如果您的 fp_len 太大,您可能会进入堆栈溢出。

char *text = malloc(sizeof(*text) * (fp_len + 1));

并且不要忘记包含 stdlib.h

关于c - 害怕。寻找。告诉。 "warning: value computed is not used ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44966190/

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