2016-05-24 18 views
1

Linuxではこのコードが動作します。ファイルから50000の整数を読み込みます(ファイルサイズ= 200000バイト)。しかし、ビジュアルスタジオ2015 freadは73を返します。 なぜ私はundestandをしないのですか?C. freadはWindowsではなくLinux上で動作します

コンソール:

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; 
} 

答えて

2

あなたがバイナリモード「RB」ファイルを開くときに、それ以外の場合は、(Windowsの場合)テキストファイルとして開きますを設定する必要があるかもしれませ対Linuxのバイナリファイル。

+0

ありがとうございます。はい。それは助けになった。 –

関連する問題