background preloader

C++ file management

Facebook Twitter

Huge file _fseeki64 _ftelli64 in Visual C++ Writing cross-platform huge file I/O code is tricky because the 64-bit offset versions of fseek and ftell are not standard across compilers and platforms.

Huge file _fseeki64 _ftelli64 in Visual C++

This article documents the CMarkup experience with this, related discoveries made along the way, and it may be useful to anyone dealing with this issue. 32-bit and 64-bit file offsets The limit of signed 32-bit integer offsets in ftell and fseek is 2^31-1 = 2147483647 which is around 2GB. For example, a common cross-platform way of getting file size is fseek with SEEK_END and then ftell, but ftell cannot return a number higher than the limit of its return type.

The 64-bit versions of these functions deal in offsets of billions of gigabytes (8 exabytes) which should be enough for single file sizes in the next decade or two ;). There are no 64-bit offset versions of fread and fwrite functions. I've seen #ifdef WIN64 used in conjunction with _fseeki64. Huge files and CMarkup off_t Unfortunately VC++ doesn't use this system. Old line 223: _lseek, _lseeki64 (CRT) Huge file _fseeki64 _ftelli64 in Visual C++ FindFirstFile function. Searches a directory for a file or subdirectory with a name that matches a specific name (or partial name if wildcards are used).

FindFirstFile function

Syntax HANDLE WINAPI FindFirstFile( _In_ LPCTSTR lpFileName, _Out_ LPWIN32_FIND_DATA lpFindFileData ); Parameters lpFileName [in] The directory or path, and the file name, which can include wildcard characters, for example, an asterisk (*) or a question mark (?). This parameter should not be NULL, an invalid string (for example, an empty string or a string that is missing the terminating null character), or end in a trailing backslash (\). If the string ends with a wildcard, period (.), or directory name, the user must have access permissions to the root and all subdirectories on the path. lpFindFileData [out] Return value Remarks The following list identifies some other search characteristics: As stated previously, you cannot use a trailing backslash (\) in the lpFileName input string for FindFirstFile, therefore it may not be obvious how to search root directories.

WIN32_FIND_DATA structure. Syntax typedef struct _WIN32_FIND_DATA { DWORD dwFileAttributes; FILETIME ftCreationTime; FILETIME ftLastAccessTime; FILETIME ftLastWriteTime; DWORD nFileSizeHigh; DWORD nFileSizeLow; DWORD dwReserved0; DWORD dwReserved1; TCHAR cFileName[MAX_PATH]; TCHAR cAlternateFileName[14]; } WIN32_FIND_DATA, *PWIN32_FIND_DATA, *LPWIN32_FIND_DATA; Members dwFileAttributes The file attributes of a file.

WIN32_FIND_DATA structure

The FILE_ATTRIBUTE_SPARSE_FILE attribute on the file is set if any of the streams of the file have ever been sparse. ftCreationTime If the underlying file system does not support creation time, this member is zero. ftLastAccessTime For a file, the structure specifies when the file was last read from, written to, or for executable files, run. For a directory, the structure specifies when the directory is created. On the FAT file system, the specified date for both files and directories is correct, but the time of day is always set to midnight. ftLastWriteTime.