All I am trying to do is put the contents of a text file in a WIN32 listbox. When I run the following code, I get lines of "||||" in the listbox. The console 'cout' function prints out the correct text from the file. What conversion is needed to get a readable listing? Is 'sendmessage' the only way to put text into a listbox?
As, going by the pointers, you're using C++/CLI, you do have another way. Rather than mixing managed pointers and old-school WIN32, you could switch to WinForms, and use code like
if anyone is interested I found one way to get the desired output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
while ((line = sr->ReadLine()) != nullptr)
{
line_lenght = line->Length;
char *lpBuffer = newchar[256];
for (int t =0;t<256;t++)//clear out the garbage
lpBuffer[t] = (WCHAR)' ';
if (line_lenght > 0)// must have some data
{
for (int t =0;t<line_lenght;t++)// replace a lpBuffer char with a line char, one char at a time
lpBuffer[t] = line[t];
}
SendMessage(hlistBox, LB_ADDSTRING, NULL, (LPARAM)lpBuffer);
delete [] lpBuffer;
index++;
1. I don't get the WCHAR cast? lpBuffer is a pointer to a char buffer, so why is it needed?
2. Why set all elems to a space rather than to '\0'?
3. Do you need to new and delete a 256 long buffer for every single line, rather than allocate it once and just clear it each time? (For such a small buffer I'd leave it on the stack).
4. Why not use strcpy()? Are you copying Unicode wchar_ts into a char buffer???
My original objective is to simply list the contents of a ascii text file in some kind of closable window. The window could be 'notepad ' if I can figure how to use it in my WIN32 app. The lines never exceed 100 characters, up to 900 lines (the lines are are machine instructions).
I have settled on WIN32 for the apps because WIN32 gives me the graphics I need. I have tried visual basic, opengl, java and several other envirements.
andywestkem:
You have good questions. WCHAR cast? In earlier attempts I kept getting conversion errors. I not sure how the code even works!. I found the lpBuffer code online somewhere. The lpBuffer code was used once to store a filename ,then send it to a listbox.
I added the following trying to get something to work.
1 2 3 4 5 6 7
for (int t =0;t<256;t++)//clear out the garbage
lpBuffer[t] = (WCHAR)' ';
if (line_lenght > 0)// must have some data
{
for (int t =0;t<line_lenght;t++)// replace a lpBuffer char with a line char, one char at a time
lpBuffer[t] = line[t];
}
I have searched and searched for a way to send multiple lines to a WIN32 listbox.
If either of you have better code, please let me know. I don't understand all of the c++ stuff yet. There should "trial and error" before my ID.
Texan:
The "std::string lineBuffer;" code works flawlessly. I added "inputFile.close()". I hope this is correct. Is there some reading material you can recommend on C++? You program for a living?