user4137741
user4137741

Reputation:

How to get file information?

I have an existing file on the computer and I was wondering if it is possible to know when it was made, Size of the file, and more properties on the file..
I tried to use in ifstream But there's the information I have on file
(I'm using Visual C++ 6.0,Cannot using Boost)

Upvotes: 2

Views: 14840

Answers (2)

Lukas Thomsen
Lukas Thomsen

Reputation: 3207

Look at function GetFileAttributesEx.

#include <windows.h>

WIN32_FILE_ATTRIBUTE_DATA fInfo;

GetFileAttributesEx("test.dat", GetFileExInfoStandard, &fInfo);

The WIN32_FILE_ATTRIBUTE_DATA contains a lot of the "common" file informations (size, creation/edit time, attributes).

Update: I just saw, that you're using Visual C++ 6. Since GetFileAttributesEx is supported since Windows XP it might not be available in your WIN API headers... You can use the function by dynamic linking. The following code does the same thing as the snippet from above:

/* clone definition of WIN32_FILE_ATTRIBUTE_DATA from WINAPI header */
typedef struct file_info_struct
{
    DWORD    dwFileAttributes;
    FILETIME ftCreationTime;
    FILETIME ftLastAccessTime;
    FILETIME ftLastWriteTime;
    DWORD    nFileSizeHigh;
    DWORD    nFileSizeLow;
} FILE_INFO;

/* function pointer to GetFileAttributesEx */
typedef BOOL (WINAPI *GET_FILE_ATTRIBUTES_EX)(LPCWSTR lpFileName, int fInfoLevelId, LPVOID lpFileInformation);


HMODULE hLib;
GET_FILE_ATTRIBUTES_EX func;
FILE_INFO fInfo;

hLib = LoadLibrary("Kernel32.dll");
if (hLib != NULL)
{
    func = (GET_FILE_ATTRIBUTES_EX)GetProcAddress(hLib, "GetFileAttributesExW");
    if (func != NULL)
    {
        func("test.dat", 0, &fInfo);
    }

    FreeLibrary(hLib);

    /*
    ** Don't call func after FreeLibrary !!!
    ** It should be ok since kernel32.dll is loaded by your application anyway but if
    ** you get a function pointer from a dll only loaded by LoadLibrary the function
    ** pointer is invalid once the library if freed.
    */
}

Upvotes: 5

Jerry Coffin
Jerry Coffin

Reputation: 490158

The size and creation data (and more) are available via FindFirstFile.

Upvotes: 2

Related Questions