在WINDOWS平台上测试了一下文件读取的各种函数,发现只有WIN的API是没错误的。
1. 下面的代码的功能是从文件尾开始反向读取文件。
InBlock.gif#include <iostream>    
InBlock.gif
InBlock.gif#include <fstream>    
InBlock.gif
InBlock.gif void test_cpp_ifs() {    
InBlock.gif                std::ifstream ifs;    
InBlock.gif                ifs.open( "./test_pdf_reader.pdf");    
InBlock.gif                ifs.seekg(-1, std::ios::end);    
InBlock.gif                 char ch;    
InBlock.gif                 for ( int i = 0; i < 500; i++) {    
InBlock.gif                                ifs.get(ch);    
InBlock.gif                                std::cout<<ch;    
InBlock.gif                                ifs.seekg(-2, std::ios::cur);    
InBlock.gif                }    
InBlock.gif}    
InBlock.gif
InBlock.gif#include <stdio.h>    
InBlock.gif
InBlock.gif void test_c_io() {    
InBlock.gif                FILE *file = fopen( "./test_pdf_reader.pdf", "r");    
InBlock.gif                fseek(file, -1, SEEK_END);    
InBlock.gif                 char ch;    
InBlock.gif                 for ( int i = 0; i < 500; i++) {    
InBlock.gif                                fread(&ch, 1, 1, file);    
InBlock.gif                                printf( "%c", ch);    
InBlock.gif                                fseek(file, -2, SEEK_CUR);    
InBlock.gif                }    
InBlock.gif}    
InBlock.gif
InBlock.gif#include <windows.h>    
InBlock.gif
InBlock.gif void test_win_file_api() {    
InBlock.gif                HANDLE h_file = CreateFile( "./test_pdf_reader.pdf",        
InBlock.gif                                GENERIC_READ,    
InBlock.gif                                FILE_SHARE_READ,    
InBlock.gif                                NULL,    
InBlock.gif                                OPEN_EXISTING,    
InBlock.gif                                FILE_ATTRIBUTE_NORMAL,    
InBlock.gif                                NULL);    
InBlock.gif                SetFilePointer(h_file, -1, 0, FILE_END);    
InBlock.gif                DWORD processed = 0;    
InBlock.gif                 char ch;    
InBlock.gif                 for ( int i = 0; i < 500; i++) {    
InBlock.gif                                ReadFile(h_file, &ch, 1,        &processed, NULL);    
InBlock.gif                                std::cout<<ch;    
InBlock.gif                                SetFilePointer(h_file, -2, 0, FILE_CURRENT);    
InBlock.gif                }    
InBlock.gif}    
InBlock.gif
InBlock.gif int main( int argc, char* argv[]) {    
InBlock.gif                test_cpp_ifs(); std::cout<<std::endl;    
InBlock.gif                test_c_io(); std::cout<<std::endl;    
InBlock.gif                test_win_file_api(); std::cout<<std::endl;    
InBlock.gif                
InBlock.gif                 return 0;    
InBlock.gif}
 
2. 下面是要读的文件的内容。
endobj
xref
0 53
0000000000 65535 f    
0000000015 00000 n    
0000149561 00000 n    
0000148916 00000 n    
0000000063 00000 n    
0000003212 00000 n    
0000018467 00000 n    
0000090352 00000 n    
0000130050 00000 n    
0000003232 00000 n    
0000149021 00000 n    
0000003307 00000 n    
0000006256 00000 n    
0000006277 00000 n    
0000149129 00000 n    
0000006353 00000 n    
0000010118 00000 n    
0000010139 00000 n    
0000149237 00000 n    
0000010215 00000 n    
0000013397 00000 n    
0000013418 00000 n    
0000149345 00000 n    
0000013494 00000 n    
0000016049 00000 n    
0000016070 00000 n    
0000149453 00000 n    
0000016146 00000 n    
0000018211 00000 n    
0000018232 00000 n    
0000018308 00000 n    
0000021398 00000 n    
0000018614 00000 n    
0000021038 00000 n    
0000021017 00000 n    
0000021206 00000 n    
0000090329 00000 n    
0000090307 00000 n    
0000091935 00000 n    
0000090510 00000 n    
0000091178 00000 n    
0000091158 00000 n    
0000091725 00000 n    
0000130028 00000 n    
0000130006 00000 n    
0000131155 00000 n    
0000130213 00000 n    
0000130632 00000 n    
0000130612 00000 n    
0000130933 00000 n    
0000148894 00000 n    
0000148872 00000 n    
0000149650 00000 n    
trailer
<</Size 53
/Root 52 0 R
/Info 30 0 R
/ID[<30A3A3F6D9647A46DEB258D289339119><30A3A3F6D9647A46DEB258D289339119>]
>>
startxref
149698
%%EOF
 
3. 下面是三个函数读取的结果。
C++的:
FE%89
ra>992A6012E43<Ro ozl000090060
013900
0900000 8301200000
n 00000 4168100000
n 00000 8931200000
n 00000 8038100000
n 00000 2328100000
n 00000 1128100000
n 00000 6416100000
n 00000 3549410000
n 00000 0706100000
n 00000 9406100000
n 00000 4943100000
n 00000 5439410000
n 00000 8143100000
n 00000 7933100000
n 00000 5120100000
n 00000 7329410000
n 00000 9310100000
n 00000 8110100000
n 00000 3536000000
n 00000 9219410000
n 00000 7726000000
n 00000 6526000000
n 00000 703300
 
C语言的:
FE%89
ra>992A6012E43<Ro ozl000090060
013900
0900000 8301200000
n 00000 4168100000
n 00000 8931200000
n 00000 8038100000
n 00000 2328100000
n 00000 1128100000
n 00000 6416100000
n 00000 3549410000
n 00000 0706100000
n 00000 9406100000
n 00000 4943100000
n 00000 5439410000
n 00000 8143100000
n 00000 7933100000
n 00000 5120100000
n 00000 7329410000
n 00000 9310100000
n 00000 8110100000
n 00000 3536000000
n 00000 9219410000
n 00000 7726000000
n 00000 6526000000
n 00000 703300
 
WIN API的:
FOE%%
896941
ferxtrats
>>
]>911933982D852BED64A7469D6F3A3A03<>911933982D852BED64A7469D6F3A3
R 0 03 ofnI/
R 0 25 tooR/
35 eziS/<<
reliart
n 00000 0569410000
n 00000 2788410000
n 00000 4988410000
n 00000 3390310000
n 00000 2160310000
n 00000 2360310000
n 00000 3120310000
n 00000 5511310000
n 00000 6000310000
n 00000 8200310000
n 00000 5271900000
n 00000 8511900000
n 00000 8711900000
n 00000 0150900000
n 00000 5391900000
n 00000 7030900000
n 00000 9230900000
n 00000 60212