Writing a robust file size function is a good exercise for beginning programmers. It requires understanding of many of the basic operating system and programming language fundamentals and there are many ways that it can be achieved. We will be looking at 2 ways:
Windows has its own set of file handling functions, dating back to Win16 and Win32 API programming days. Recently, the implementations changed a little, and it became commonplace to use the Standard C IO functions (from stdio.h) as in Unix based operating systems. Nonetheless, the Windows API function calls are still recommended for compatibility reasons.
If the language or API does not have a File Size function, then the basic theory runs like this:
Refinements to this include being able to pick up the file size from any position, and restore it if necessary. This is useful when a file is open for reading and appending; something that the standard C IO routines implement but that other languages and operating systems might not.
The Windows API (since WinNT / Win32) has a function to get the file size directly, for any file that has been opened using the CreateFile API function. CreateFile is necessary because it returns a HANDLE which can be used in other Windows file handling API functions.
The actual definition of GetFileSize is:
The HANDLE is self-explanatory, but lpHugeSize might need some explanation. In fact, a DWORD can only contain a file size of up to 32 bits (2^32 = 4GB). This is probably as large as anyone would need, but just in case it is not, Microsoft has allowed a second DWORD to be supplied to get the high order 32bits, thereby allowing for a much larger file size.
As long as the programmer is sure that the file size will not exceed 4GB, they can supply NULL as the lpHugeSize parameter, and will be returned the file size as a DWORD. Otherwise, they must supply a pointer to a DWORD variable to store the value in.
The stdio.h library provides several functions for getting information about files, but lacks one to actually return the size of a file. However, we can implement our own function MyGetFileSize to deal with it.
Since we can open a file in C using the flags "a+" for 'append and read', it makes sense to provide a file size function that works without destroying the current file position. We can append to the file, as well as read from it, and so it might grow over time, hence the necessity for a file size function, and the reason why we do not want to destroy the file pointer in the process.
We use fseek to move the pointer through the file. It takes the file handle, the offset, and the origin (0 = start, 1 = current position, 2 = the end) as parameters. Once we have repositioned the pointer to the end, we can use ftell to get the pointer location, from the beginning of the file. The function itself is reasonably simple:
A simple call to MyGetFileSize, with the file handle, will return the file size.
Many other languages are likely to have their own File Size functions, but the above processes can be used if not. Windows programmers always have the API functions available to them, and other operating systems usually support file seeking.