Thread: GetFileSize

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    17

    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];
       
       //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);
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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.
    Last edited by Ancient Dragon; 06-30-2006 at 11:08 AM.

  3. #3
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    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. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    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.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    Thanks for your help.

    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. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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:
    Quote 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.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    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. #10
    Registered User
    Join Date
    Jun 2006
    Posts
    17
    I found the error:

    Code:
    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);
    }
    This needed to be LPCWSTR. It all works fine now. Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GetFileSize() and directories
    By aLiNuSh in forum Windows Programming
    Replies: 8
    Last Post: 02-09-2008, 11:44 AM
  2. GetFileSize
    By Tropicalia in forum C++ Programming
    Replies: 6
    Last Post: 09-24-2006, 11:10 AM