在WINDOWS平台上测试了一下文件读取的各种函数,发现只有WIN的API是没错误的。
1. 下面的代码的功能是从文件尾开始反向读取文件。
#include <iostream> #include <fstream> void test_cpp_ifs() { std::ifstream ifs; ifs.open( "./test_pdf_reader.pdf"); ifs.seekg(-1, std::ios::end); char ch; for ( int i = 0; i < 500; i++) { ifs.get(ch); std::cout<<ch; ifs.seekg(-2, std::ios::cur); } } #include <stdio.h> void test_c_io() { FILE *file = fopen( "./test_pdf_reader.pdf", "r"); fseek(file, -1, SEEK_END); char ch; for ( int i = 0; i < 500; i++) { fread(&ch, 1, 1, file); printf( "%c", ch); fseek(file, -2, SEEK_CUR); } } #include <windows.h> void test_win_file_api() { HANDLE h_file = CreateFile( "./test_pdf_reader.pdf", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); SetFilePointer(h_file, -1, 0, FILE_END); DWORD processed = 0; char ch; for ( int i = 0; i < 500; i++) { ReadFile(h_file, &ch, 1, &processed, NULL); std::cout<<ch; SetFilePointer(h_file, -2, 0, FILE_CURRENT); } } int main( int argc, char* argv[]) { test_cpp_ifs(); std::cout<<std::endl; test_c_io(); std::cout<<std::endl; test_win_file_api(); std::cout<<std::endl; return 0; }
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