2

So, I'm running on Windows and need to figure out the size of a file.

SO mentions GetFileSize in many places, which takes a HANDLE. I'm used to just working with FILEs returned from fopen and such.

  1. What is the difference between FILE and the Windows HANDLE? When should each be used?
  2. For example, can I pass a plain FILE obtained from fopen to GetFileSize? Or do I need to use the Windows CreateFile (?) function to get a HANDLE and then pass it in?
Richard Chambers
  • 14,509
  • 3
  • 62
  • 86
Aviv Cohn
  • 11,431
  • 20
  • 52
  • 95
  • https://stackoverflow.com/questions/2423628/whats-the-difference-between-a-file-descriptor-and-file-pointer – ghost_duke Nov 13 '19 at 16:07
  • also GetFileSize is a windows only function, not standard c – ghost_duke Nov 13 '19 at 16:09
  • worth mentioning that the `stdio.h` closest equivalent (which will let you pass a `FILE*`) is `fseek(SEEK_END)` followed by `ftell()`. And the equivalent on other OSes (which is in the POSIX standard but not the C standard, and does not use `FILE*`) is `fstat()`. – Ben Voigt Nov 13 '19 at 17:00

2 Answers2

3

FILE is a type of the standard C library, used exclusively for file handling with the stdio.h functions. It cannot be used for anything else. The C standard lib has the advantage of being portable.

HANDLE is a similar but wider concept used by the Windows API, used for everything where they want an abstraction layer on top of raw pointers. It is used for files, ports, threads, mutex, events, pipes and so on. Generally: any Windows resource that you can WaitForSingleObject on - the WaitFor... functions being Window's way of letting a thread effectively sleep without using up CPU resources. See What is a Windows Handle?. It only works for Windows API specific-functions like CreateFile.

Both of these are "opaque types" that the application programmer doesn't know or care about how they are implemented internally.

In practice, the standard C library port for Windows is just an abstraction layer on top of the Windows API. So FILE* fp = fopen("", "w"); will very likely boil down to a CreateFile call internally. So for all we know, FILE could contain a HANDLE internally.

Lundin
  • 155,020
  • 33
  • 213
  • 341
  • https://stackoverflow.com/questions/3989545/how-do-i-get-the-file-handle-from-the-fopen-file-structure – Antti Haapala Nov 13 '19 at 16:40
  • `fopen` calls `_open`. The latter returns a C file descriptor. This in turn wraps a Windows File handle, which comes from an internal `CreateFileW` call. A File handle refers to a kernel File object. Handles for kernel objects also include the access granted to the object at the time the handle was opened, along with flags that determine whether the handle can be closed via `CloseHandle` and whether it can be inherited by a child process. – Eryk Sun Nov 13 '19 at 16:58
1

The GetFileSize() function with HANDLE are part of the Windows API which means that it is only usable with a Windows program.

The fopen() and FILE are part of the C Runtime library that comes with any standard C compiler which means that using those should be operating system independent.

You can not pass a FILE to the GetFileSize() function because a HANDLE and a FILE are two different things.

Richard Chambers
  • 14,509
  • 3
  • 62
  • 86