open file error

This is a discussion on open file error within the Windows Programming forums, part of the Platform Specific Boards category; hi all, i 'm working on mfc program and read text file using createfile() Code: CString debug; CString myfile; myfile ...

1. 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,
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. 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).

3. but i still cannot open the file when i cancel the getbuffer method...

4. 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. Does the file you're trying to open actually exist? That sounds like the problem to me.

6. my file does existed in this path.
how's your problem? also the system can't find the file?

7. From reading this, I can't figure out why you're passing some ridiculously large number like 10000 to getbuffer.

8. 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";}

9. Originally Posted by novacain
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";}

the :: makes no difference.

10. 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. Perhaps your smart device doesn't have a D: drive.

12. i use emulator to run the application on window, so the d drive exists, and the file path is right...

13. 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. 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)

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