GetFileSize

This is a discussion on GetFileSize within the C++ Programming forums, part of the General Programming Boards category; Hey guys, im trying to use the getfilesize win32api function, but it is returning -1, why? (test.txt has 17 bytes). ...

  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,796
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,443
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 10:44 AM
  2. GetFileSize
    By disruptor108 in forum Windows Programming
    Replies: 9
    Last Post: 06-30-2006, 02:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21