# GetFileSize

This is a discussion on GetFileSize within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to check the validity of a file by getting its size, but when I check for the size ...

1. ## GetFileSize

I'm trying to check the validity of a file by getting its size, but when I check for the size of this file the size is always 0xFFFFFFFF meaning that the file is invalid. I have the file in the same directory as my source files, so I don't understand why this code shouldn't work. Any suggestions?

This code compiles fine, I've tried to remove all the useless clutter I've been testing to make it easier to look at:

Code:
#include <stdio.h>

HANDLE OpenFile(char *FileName, DWORD AccessMode);

int main(int argc, char* argv[])
{
DWORD   Length, InFileSize;
HGLOBAL FileDataPtr;

char infile[80];
char outfile[80];

HANDLE hFile;
DWORD wmWritten;
char strVal[1024];
CloseHandle(hFile);

printf(strVal);

//OutFile   = OpenFile(argv[2], GENERIC_WRITE);
OutFile   = OpenFile("out.txt", GENERIC_WRITE);

if((InFile) && (OutFile))
{
InFileSize = GetFileSize(InFile, &Length);
printf("%lu", InFileSize);

if(InFileSize)
{
FileDataPtr = GlobalAlloc(GMEM_FIXED, InFileSize);
if(FileDataPtr)
{
printf("here in file_data_ptr\n");
{
//Level = 0;
ParseFileData((char *)FileDataPtr, InFileSize);
}
GlobalFree(FileDataPtr);
}
}
}
if(InFile)
{
CloseHandle(InFile);
}
if(OutFile)
{
CloseHandle(OutFile);
}
return 0;
}

HANDLE OpenFile(char *FileName, DWORD AccessMode)
{
HANDLE FileHandle;
DWORD  CreateAttribute;

FileHandle = CreateFile((LPCWSTR)FileName, AccessMode, FILE_SHARE_READ, NULL, CreateAttribute, FILE_ATTRIBUTE_NORMAL, NULL);
return(FileHandle);
}

2. check the return value of CreateFile() to see that the file was opened ok. CreateFile() does NOT return NULL if the open failed -- see MSDN. If not then anything you do with that invalid handle is worthless.

3. Like Ancient Dragon said, check the return value of CreateFile to ensure you have a valid file handle(not INVALID_HANDLE_VALUE) prior to using that handle. If the function fails to return a valid handle, you can use GetLastError to get more information about the cause of that failure.

4. I get a ERROR_FILE_NOT_FOUND error. This doesn't make any sense, shouldn't I have this txt file located in the same place as my source files?

(ex. C:\...\WinParse\WinParse)

5. Some ides use the project directory as the current directory which may not necessarily be the same directory as where the executable is built. This is true if you run the program from within the ide. If you run the program independently then it will use that directory as the current working directory as you would expect.

A workaround for the ide behaviour is to keep a copy of the 'in.txt' file in the project directory, or run the executable itself, making sure a copy of the file to be read is in the same directory. Alternatively, explicitly specify the full path name to the file you wish to open.

6. Another problem is windows does not garentee the place where you ran the program is the current working directory. Even if you go to the file in explorer and run it.

Alright, I'm using VS 2005 on XP, so I don't think that the IDE is the problem. Besides, when I tested opening the file and reading it without running it through the function the file read and displayed correctly to the console.

I changed my input file to:

Code:
InFile    = OpenFile("C:\Documents and Settings\awesthusing.SS1\My Documents\Programming\C\WinParse\WinParse\in.txt", GENERIC_READ);
Yet I still get the same error....weird

8. Since you're opening the file for read/write, I'd try changing the 'FILE_SHARE_READ' flag(3rd parameter) to FILE_SHARE_READ | FILE_SHARE_WRITE:
Originally Posted by msdn
f this flag is not specified, but the object has been opened for write access, the function fails.
I hope that helps - I'm off to watch Italy v Ukraine now.

9. Code:
InFile    = OpenFile("C:\Documents and Settings\awesthusing.SS1\My Documents\Programming\C\WinParse\WinParse\in.txt", GENERIC_READ);
That should have given you several compiler errors. you need to double backslash them
Code:
InFile    = OpenFile("C:\\Documents and Settings\\awesthusing.SS1\\My Documents\\Programming\\C\\WinParse\\WinParse\\in.txt", GENERIC_READ);

10. I found the error:

Code:
HANDLE OpenFile(char *FileName, DWORD AccessMode)
{
HANDLE FileHandle;
DWORD  CreateAttribute;

}