FILE *infp, *outfp;
infp = fopen(argv[2], "r");
int len;
char *text;
fseek(infp, 0, SEEK_END);
len = ftell(infp);
printf("%d\n", len);
if ((text = (char *) malloc(500000000)) == NULL)
{
fprintf(stderr, "Error allocating memory\n");
exit(1);
}
fread(text, len, 1, infp);
text[len] = '\0';
fclose(infp);
printf("Text = %s, Address = %u\n", text, text);
退货
138
Text = , Address = 3794927632
我不确定为什么文本没有打印任何内容。我是否以某种方式使用fread错误?
最佳答案
后
fseek(infp, 0, SEEK_END);
infp
指向文件的末尾。您需要倒带文件。rewind(infp);
有关其他信息,请参见http://www.cplusplus.com/reference/cstdio/rewind/。
关于c - fread/malloc问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32956257/