Thread: GetFileSize

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    57

    GetFileSize

    Hey guys, im trying to use the getfilesize win32api function, but it is returning -1, why? (test.txt has 17 bytes).

    Code:
    int main(){
    HANDLE hFile;
    hFile = "c:\\teste.txt";
    int filesize = GetFileSize(hFile, NULL);
    cout << filesize << endl;
    system ("PAUSE");
    }
    Thanks

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try using GetLastError to find out the exact reason why it failed.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Code:
    int main(){
    HANDLE hFile;
    hFile = "c:\\teste.txt";
    int filesize = GetFileSize(hFile, NULL);
    if (filesize == 0xFFFFFFFF) cout << "error: " << GetLastError() << endl; 
    else cout << filesize << endl;
    system ("PAUSE");
    }
    Well, i dont know too much about that function, but it outputed a "6".

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well reading this
    http://msdn.microsoft.com/library/de...tlasterror.asp

    Would lead you to this
    http://msdn.microsoft.com/library/de...rror_codes.asp

    One more click, and you're there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Error 6: ERROR_INVALID_HANDLE - The handle is invalid.

    Why my handle is invalid? =~~

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Probably because just pointing at a filename isn't enough
    Maybe open the file using a win32 API function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    57
    Yay, CreateFile()
    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 disruptor108 in forum Windows Programming
    Replies: 9
    Last Post: 06-30-2006, 02:33 PM