今天去面試,面試官問了個C中如何獲取文件大小,想啊想,沒有這個函數啊。那應該是用fseek將文件指針移動到文件末尾,然后通過文件指針獲取吧?但是什么函數呢,沒印象,汗......
回來查了下,原來是ftell,悲劇啊,以前沒用過!MSDN上ftell的功能如下:Gets the current position of a file pointer。寫了段代碼測試了下,代碼如下:
#include <stdio.h>

int main()


{
long fileSize = 0;
FILE* pFile = fopen( "mm.data", "r" );
if ( pFile == NULL )

{
printf( "Open File Error\n" );
}
else

{
// 將指針定位到文件末尾
fseek( pFile, 0L, SEEK_END );
fileSize = ftell( pFile );
printf( "file size: %ld\n", fileSize );
}
return 0;
}
回來查了下,原來是ftell,悲劇啊,以前沒用過!MSDN上ftell的功能如下:Gets the current position of a file pointer。寫了段代碼測試了下,代碼如下:


























