So you are passing some data block to a system call, that isn't the right size. What is the system call on the line before your error?
--
Mats
So you are passing some data block to a system call, that isn't the right size. What is the system call on the line before your error?
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I'm not entirely confident that the CString default constructor doesn't do something. Place that declaration before the CreateFile call and try again.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
What is the system call on the line before your error?>
what does that mean?
i put following code:
and when i click on the item on the listbox, it will connect to the txt file to display the content for that item, but when i click the item i got the error msg and display step 12.Code:HANDLE hFile = CreateFile(TEXT("D:\\file.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); CString debug; if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid DWORD dw = GetLastError (); debug = "Step 12"; detail.DetailValue = debug; } detail.DoModal();
should i add some code like free the buffer and how? thanks!!
GetBuffer returns a char* of the specified size from the CString object. See documentation.Code:CString my_str; CListBox m_list; // Alternatively: CString m_list; // Assuming you've opened the file with an istream obect named my_file my_file.readline(my_str.GetBuffer(10000), 10000); my_str.ReleaseBuffer(); m_list.AddString(my_str);
readline reads a line from the input file, see basic_istream and readline in docs.
ReleaseBuffer is required after you've called GetBuffer to read data in a CString object.
Then just add the string to the listbox.
istream doesn't support reading into CString by default. There are other ways to do it, I'm sure, though.
Last edited by Elysia; 11-20-2007 at 03:12 AM.
actually i code this to test:
but it still can't open the file, i doubt that is still the buffer problem, i really dun know how to solve this problem, do u know how ? thanks a lot!!Code:string line; ostringstream cout; ifstream myfile ("D:\\file.txt"); if (myfile.is_open()) { while (! myfile.eof() ) { getline (myfile,line); cout << line << endl; string line2 = cout.str(); const char* line3 = line2.c_str(); detail.DetailValue = line3; } myfile.close(); } else detail.DetailValue = "Unable to open file"; detail.DoModal(); }
Well, for one, do the actual file actually exist?
And your code is poor is so many ways - does it even compile?
First, cout is a reserved global ostream object - it's bad to hide it with your local varible, choose another name.
while(! myfile.eof() ) is bad. See FAQ.
getline is not a global function, it's part of the basic_istream object (myfile).
cout.str() <--- ???? str() isn't even a member function of a basic_ostream object! Not to mention you're trying to so WHAT? Read the contents of the written file or something?
the file exists, and those coding can be compiled, anyway, i 've modified the coding:
is that correct? but the result i got is no response instead of display step 12..Code:CString debug; CString myfile; myfile = "\\D:\\file.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 = "Step 12"; detail.DetailValue = debug; } }
could u suggest any coding to me to cope with it? exhausted..help!!and thanks very much!!
Are you by any chance using uni-code? Perhaps you want the TCHAR *file = TEXT("filename") instead of your version. If you look at either the pre-processed output or assembler listing from the compiler, you should see if it calls CreateFileA or CreateFileW.
And you still need either no backslashes at the front of the name, or another two - so that after the compiler has removed the escape ones, you end up with two backslashes at the front of the string.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
For the error - I have no idea, but as matsp indiciates "\D:\File.txt" is an invalid filename. CreateFile will fail with error 123 - invalid filename.
Aside from that, what is D:\? Is it a hard disk? A floppy drive? A CD-ROM? A USB drive?