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];
//Reads the file correctly.
HANDLE hFile;
DWORD wmWritten;
char strVal[1024];
hFile = CreateFile(TEXT("in.txt"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
ReadFile(hFile,strVal,1024,&wmWritten,NULL);
CloseHandle(hFile);
printf(strVal);
//InFile = OpenFile(argv[1], GENERIC_READ);
//OutFile = OpenFile(argv[2], GENERIC_WRITE);
InFile = OpenFile("in.txt", GENERIC_READ);
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");
if(ReadFile(InFile, FileDataPtr, InFileSize, &Length, NULL))
{
//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;
CreateAttribute = (AccessMode == GENERIC_READ)?OPEN_EXISTING:CREATE_ALWAYS;
FileHandle = CreateFile((LPCWSTR)FileName, AccessMode, FILE_SHARE_READ, NULL, CreateAttribute, FILE_ATTRIBUTE_NORMAL, NULL);
return(FileHandle);
}