Thread: open file error

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    74

    open file error

    hi all,

    i 'm working on mfc program and read text file using createfile()
    Code:
    CString debug;
    CString myfile;
    myfile = "\\D:\\text.txt";
    HANDLE hFile = CreateFile(myfile.GetBuffer(10000), GENERIC_READ | GENERIC_WRITE, 
                       FILE_SHARE_READ,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    myfile.ReleaseBuffer();
    	
    
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "error";
    		detail.DetailValue = debug;
    	}else{detail.DetailValue = "open file succed";}
    the file is not opened succed, and i caought the error msg: the data area passed to the system call is too small.
    does anyone know this problem?
    and can anyone provide succeeded way for reading file?
    thanks much!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So why are you trying to use CreateFile in MFC again?
    Code:
    myfile = "\\D:\\text.txt";
    Invalid. Remove the "\\" before "D:\\".
    Code:
    HANDLE hFile = CreateFile(myfile.GetBuffer(10000), GENERIC_READ | GENERIC_WRITE,
    Not necessary. You only need to call GetBuffer if you need to pass a char*, but CreateFile expects a const char*, which is completely different. Just pass myfile.
    Code:
    myfile.ReleaseBuffer();
    Again, get rid of that. It's not necessary if you don't call GetBuffer - and even if you do, it is only necessary if you modify the string buffer (which CreateFile doesn't).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    but i still cannot open the file when i cancel the getbuffer method...

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    now i use open() instead of createfile(), and it display the error: the system cannot find the path specified.
    my path is: D:\\text.txt
    my file does existed in this path.
    does anyone know how to solve this?

    thanks!

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Does the file you're trying to open actually exist? That sounds like the problem to me.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    my file does existed in this path.
    how's your problem? also the system can't find the file?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    From reading this, I can't figure out why you're passing some ridiculously large number like 10000 to getbuffer.
    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.

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try,
    after ensuring you can create / open a file at d:\

    NOTE: changed the flags on CreateFile()

    Code:
    CString debug;
    CString myfile;
    myfile = "D:\\text.txt";
    HANDLE hFile = ::CreateFile(myfile, GENERIC_READ|GENERIC_WRITE, 
                       0,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "error";
    		detail.DetailValue = debug;
    	}else{detail.DetailValue = "open file succed";}
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by novacain View Post
    Try,
    after ensuring you can create / open a file at d:\

    NOTE: changed the flags on CreateFile()

    Code:
    CString debug;
    CString myfile;
    myfile = "D:\\text.txt";
    HANDLE hFile = ::CreateFile(myfile, GENERIC_READ|GENERIC_WRITE, 
                       0,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "error";
    		detail.DetailValue = debug;
    	}else{detail.DetailValue = "open file succed";}

    thanks for reply
    the :: makes no difference.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    i found that when i use smartdevice dialogue based application and use createFile function, the function cannot work but it works in windows based, does anyone meet this situation and how to solve it?
    thanks!

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps your smart device doesn't have a D: drive.
    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.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    i use emulator to run the application on window, so the d drive exists, and the file path is right...

  13. #13
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    hi all,

    now i use the following code:

    Code:
    CString myfile = L"\\text.txt";
    HANDLE hFile = CreateFile(myfile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0,NULL);
    but i still caught the file not found error. as in pocket pc, i put the file into my device, and for running in emulator, and put the file into the c: drive, why it still can't read the file?
    thanks!

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Create a file (ie with notepad.exe) called 'text.txt'

    Place in the c:\ folder

    Use Explorer to find the file and double click to open

    [ie test the file exists and you can open it]

    change

    CString myfile = L"\\text.txt";

    to

    CString myfile = L"c:\\text.txt";

    The code should now work.

    If it does not, call GetLastError() as soon as you know the file failed to open. Check the error code returned in your compiler (MSVC->Tools->Error Lookup)

    ie 5 == ACCESS DENIED
    1006 == File not vaild
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    just pass the real path to the file:

    Code:
    CString myfile = L"c:\\text.txt";
    I don't know what you're trying to do with omitting the drive letter, but it looks like the type of thing that would upset CreateFile since "\text.txt" is not a valid file name.
    Last edited by Bleech; 12-18-2007 at 11:30 PM. Reason: novacain beat me to it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM