如果
fp_len = ftell(fp);
打印470
这不应该打印“471”吗? (它打印 8。可能只打印第一行)
char *text = malloc(sizeof(*text) * fp_len + 1);
int text_len;
text_len = sizeof(text);
printf("text-len: %d --- ",text_len);
完整:
FILE *fp;
fp = fopen(path, "r");
fseek(fp, 0, SEEK_END);
int fp_len;
fp_len = ftell(fp);
printf("%d---", fp_len);
fseek(fp, 0, SEEK_SET);
char *text = malloc(sizeof(*text) * fp_len + 1);
int text_len;
text_len = sizeof(text);
printf("text-len: %d --- ",text_len);
fread(text, fp_len, 1, fp);
printf("%s",text);
free(text);
最佳答案
sizeof
给出类型的大小,而不是字符串长度。
对于char *text
,类型是指针值,64 位机器上指针值的大小可能是 8。
关于c - sizeof(文本) 不等于 : fp_len + 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44967257/