[問題] fread() 與 ftell() 回傳值的差別
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
想請教 size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
size 與 count 的參數差異
我測試一個讀檔程式, 大致是分為這兩種讀檔方式
fread ( in_buf, 1, 10, file );
fread ( in_buf, 2, 5, file );
我使用 ftell ( file );
得到 ftell 回傳值, 這兩個都一樣, 而 fread 回傳值不同
這兩個寫法有甚麼差異?
在檔案上又有甚麼影響?
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
#include "stdafx.h"
#define BUFFER_SIZE 10
int main( int argc, char *argv[] )
{
FILE *input_file1, *input_file2;
input_file1 = fopen( argv[1], "rb" );
input_file2 = fopen( argv[2], "rb" );
unsigned char inp_buf1[BUFFER_SIZE];
unsigned char inp_buf2[BUFFER_SIZE];
fseek( input_file1, 0, SEEK_SET );
fseek( input_file2, 0, SEEK_SET );
printf( "fread1 = %d, ", fread( inp_buf1, 2, BUFFER_SIZE / 2, input_file1 ) );
printf( "ftell1 = %d\n", ftell( input_file1 ) );
printf( "fread2 = %d, ", fread( inp_buf2, 1, BUFFER_SIZE, input_file2 );
printf( "ftell2 = %d\n", ftell( input_file2 ) );
return 0;
}
補充說明(Supplement):
argv[1] 是 argv[2] 的複製檔案, 名稱不同, 檔案內容相同
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.235.201.220
→
12/01 17:10, , 1F
12/01 17:10, 1F
→
12/01 18:55, , 2F
12/01 18:55, 2F
→
12/01 18:56, , 3F
12/01 18:56, 3F
→
12/01 19:10, , 4F
12/01 19:10, 4F
→
12/01 19:10, , 5F
12/01 19:10, 5F
→
12/01 19:11, , 6F
12/01 19:11, 6F
→
12/01 19:51, , 7F
12/01 19:51, 7F
→
12/01 19:52, , 8F
12/01 19:52, 8F
→
12/01 19:53, , 9F
12/01 19:53, 9F
→
12/01 19:54, , 10F
12/01 19:54, 10F
→
12/01 20:18, , 11F
12/01 20:18, 11F
→
12/01 20:19, , 12F
12/01 20:19, 12F
→
12/01 20:20, , 13F
12/01 20:20, 13F
推
12/01 20:21, , 14F
12/01 20:21, 14F
→
12/01 20:26, , 15F
12/01 20:26, 15F
→
12/01 20:30, , 16F
12/01 20:30, 16F
→
12/01 21:54, , 17F
12/01 21:54, 17F
→
12/01 21:55, , 18F
12/01 21:55, 18F
→
12/01 21:56, , 19F
12/01 21:56, 19F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章