Are there any Windows API functions GetFilePointer (Ex)?

I am trying to debug a program that manages a file. For example, I set the file pointer to offset 4 (using base 0), but it seems to start at offset 5 instead.

To try to figure out what is going on, I want to put a line to print the current file pointer (I am not using an IDE for this small project, just Notepad2 and the command line). Unfortunately, there is no Windows API function to retrieve the current file pointer, only one to install it.

It seems to me that you can find the current file pointer in Pascal (in DOS), but how to determine the current file pointer in C ++ on Windows?

+4
source share
1 answer

Unlike most functions that provide both getter and setter (in the sense of read-write), there really is no GetFilePointer or GetFilePointerEx .

However, the value can be obtained by calling SetFilePointer (Ex) . The two SetFilePointer functions return return / output from SetFilePointer , but you must make sure that the mode is set to offset 0 , and FILE_CURRENT . Thus, it moves 0 bytes from where it is, and then returns (I cannot vouch for whether it discards the CPU and RAM cycles for zero movement, but I think they are optimized to not do this) .

Yes, this is inconsistent and confusing (and redundantly and poorly designed), but you can wrap it in your own GetFilePointer(Ex) function:

 DWORD GetFilePointer (HANDLE hFile) { return SetFilePointer(hFile, 0, NULL, FILE_CURRENT); } LONGLONG GetFilePointerEx (HANDLE hFile) { LARGE_INTEGER liOfs={0}; LARGE_INTEGER liNew={0}; SetFilePointerEx(hFile, liOfs, &liNew, FILE_CURRENT); return liNew.QuadPart; } 
+15
source

Source: https://habr.com/ru/post/1491949/


All Articles