API Reading Files, handle is always -1

This is a discussion on API Reading Files, handle is always -1 within the C++ Programming forums, part of the General Programming Boards category; Okay. I am trying to read a file in VC++ MFC using API. I don't want to use all of ...

  1. #1
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    API Reading Files, handle is always -1

    Okay. I am trying to read a file in VC++ MFC using API. I don't want to use all of this stupid C++ built in commands. I am trying to obtain a handle to a file so that I can read it later on, however It wont give me a valid handle. It keeps returning -1. I thought it might be the filename so I tried "\\" as directory changes but still didnt work. Here is the source I am trying, Its messy... but I figure it should work.

    HANDLE hHandle;
    HANDLE hTemp;
    long lHandle;
    char *sHandle;
    SECURITY_ATTRIBUTES blah;

    //UNDER HERE IS MY PROBLEMS:
    blah.nLength = sizeof(SECURITY_ATTRIBUTES);
    hHandle = CreateFile("C:\documents and settings\xei\desktop\StudyGuide.txt",
    GENERIC_READ,
    0,
    &blah,
    OPEN_ALWAYS,
    FILE_ATTRIBUTE_NORMAL,
    NULL);
    //UP THERE IS MY PROBLEMS;

    //This messages the Handle of the file. It seems to be -1 all of the time.
    lHandle = (long)hHandle;
    sHandle = new char[10];
    sHandle[9] = (char)0;
    ltoa(lHandle,sHandle,10);
    MessageBox(sHandle,NULL,MB_OK);
    CloseHandle(hHandle);
    delete [] sHandle;

  2. #2
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Hey

    Sorry, forgot to mention. For those of you who are trying to help me thank you very much. hHandle is the return handle of the file being attempted to open. Which always returns -1, when it should return something like 512, or 716 etc.. I can do it in VB just fine to try to troubleshoot parameters... but in VC++ It just doesn't seem to work. Thanks for the help.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Not sure (because I use the plain C file IO), but you may have luck by changing

    "C:\documents and settings\xei\desktop\StudyGuide.txt"

    to:

    "C:\\documents and settings\\xei\\desktop\\StudyGuide.txt"
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Sebastiani is right.

    You get -1 if your file doesn't exist or (as in your case) it can't find it so it thinks it doesn't exist.

  5. #5
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I have tried the "\\" and the file does exist. I can use the exact same API in VB and it will get the handle to that file. But VC++ will not. Do you think it could have something to do with The case of the file name? Or maybe I need the filename in a variable... or I have no idea. Thanks for the suggested solutions, they are all welcome.

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    One thing I noticed is the &blah pointer you're passing for the security attributes param. I normally pass NULL, but I don't think that's your problem either, just pointing it out.

  7. #7
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Hm...

    I tried using a NULL in placement of it. So I figured that maybe If I pass it a practically-NULL Structure then it would maybe fix my problem... but it didnt either. This is really frusterating. If anyone has source code of their own that uses this API I would be happy to look through it to see what you did right, and I did wrong. Anyways, If I happen to solve it before I goto bed... I'll tell everyone what the problem was! lol. Crap, I've gotta wake up in 4 hours to goto work :|

  8. #8
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    This is exactly what I used to test it when I first read your post.
    Works fine (returns the number 8).
    Code:
    HANDLE hHandle;
    hHandle=CreateFile("test.text",GENERIC_READ,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    long lTest=(long)hHandle;
    char cTest[20];
    _ltoa(lTest,cTest,10);
    MessageBox(NULL,cTest,"DB",NULL);
    CloseHandle(hHandle);

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    For a start, that code you initially posted wont even compile.....

    The single '\' produces illegal esacpe chars.....also, MessageBox() is not defined properly...

    The problem here might be that you are trying to create a file in a user's personal directory........if you dont have access to do this )as per your logon attributes) you might be stopped from doing so.......try it in less secure directory.....

  10. #10
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Um...

    No. Everything is defined properly as well as the MessageBox. I am using MFC and an include of "windows.h" for the API Calls, I Just didnt paste the intire cpp file so that I dont waste anyones time. I thought it might be permissions to the desktop since it is Windows XP so I figured I Might have to edit some UserTokens but then again the API did work in VB 6.0 without permissions, so there goes the idea of permissions. All code is done in VC++ .NET but that shouldn't make a difference.

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    When you get an error from a WinAPI function, always check GetLastError to see what went wrong:
    Code:
    #include <windows.h>
    
    const char* ErrorMessage(DWORD error)
    {
        static char buffer[255];
        FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, error, 0, buffer, 255, 0);
        return buffer;
    }
    
    int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
    {
        HANDLE hFile = CreateFile("c:\\myfile.txt", GENERIC_READ,
            0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if(hFile == INVALID_HANDLE_VALUE)
        {
            // Something happened - find out what
            MessageBox(0, ErrorMessage(GetLastError()), 0, MB_OK);
        }
        else
        {
            MessageBox(0, "File opened successfully.", "Success", MB_OK);
            CloseHandle(hFile);
        }
        
        return 0;
    }
    - lmov

  12. #12
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Thanks

    I'll try getting the detailed error message. Thanks.

  13. #13
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Found Problem

    For some reason... the problem was passing the Security Structure through the API. I guess I must've been doing something wrong when the parameter was actually NULL. But anyways, now we know the problem. Thanks for the help everyone.

  14. #14
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719

    Testing tags

    Code:
    aldfjadljflda
    blahbalhbal
    Last edited by Xei; 05-06-2002 at 10:19 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM

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