如何代替
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
我是一名优秀的程序员,十分优秀!