GetFileSizeEx

Hi Guys,

I'm having problems with the following code:

#define STRICT
#define WIN32_LEANANDMEAN
#define _WIN32_WINNT 0x0501

#include <windows.h>
#include <iostream>
#include <string.h>

LONGLONG getFileSize(char* fileName); // function prototype

int main(int argc, char**argv) {

std::cout << "File size is " << getFileSize("sound.wav") << std::endl;

}

LONGLONG getFileSize(char* fileName) {

/* Gets the size of the file passed in fileName */

HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ , NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

if (hFile == INVALID_HANDLE_VALUE) {
std::cout << "Failed to open file : " << fileName << std::endl;
return 0;
}

LARGE_INTEGER size;

if (!GetFileSizeEx(hFile, &size)) {
CloseHandle(hFile);
return -1; // error condition, could call GetLastError to find out more
}

CloseHandle(hFile);

// std::cout << "File size is : " << size.QuadPart << std::endl;

return size.QuadPart;
}

I have tried this code in three different IDEs and I get the same result in all
three: Even though the file to be loaded is in the same directory as the code is, I always get the 'failed to open file : sound.wav

Any suggestions?

Thx

FC


Make sure that the file is in the project directory, or the directory that the IDE wants the file to be. (This may or may not be in the source code directory, you must check it yourself.)
Topic archived. No new replies allowed.