gpt4 book ai didi

linux - C. fread 适用于 linux 但不适用于 windows

转载 作者:可可西里 更新时间:2023-11-01 11:48:26 25 4
gpt4 key购买 nike

在 linux 上,此代码有效。它从文件中读取 50000 个整数(文件大小 = 200000 字节)。但在 visual studio 2015 中,fread 返回 73。我不明白为什么?

控制台:

Oops. Can not read successfully the stack.
size = 50000
rsize = 73
filename = nums
Size of int = 4
Better to STOP
Press any key to continue . . .

代码片段:

FILE* f = fopen(fn, "r");
if (f == NULL)
{
printf("\nCan not open file %s\n", fn);
return 1;
}

int* stack = malloc(sizeof(int) * size);
if (stack == NULL)
{
fclose(f);
printf("\nCan not stack the stack - not enough memory\n");
return 2;
}


int rsize = fread(stack, sizeof(int), size, f);
if (size != rsize)
{
printf("\nOops. Can not read successfully the stack.\n size = %d\nrsize = %d\nfilename = %s\nSize of int = %d\nBetter to STOP\n", size, rsize, fn, sizeof(int));
fclose(f);
free(stack);
return 3;
}

最佳答案

您可能需要在打开文件时设置二进制模式“rb”,否则它将作为文本文件(在 Windows 中)打开,而在 Linux 中则为二进制文件。

关于linux - C. fread 适用于 linux 但不适用于 windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37425418/

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