Advertisement Jump to content


This topic is now archived and is closed to further replies.


SetDlgItemText() Oddity

This topic is 5976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have noticed something something strange when using SetDlgItemText() to put a string into a text box. What I am doing is reading a file into a buffer and then displaying the file contents in a text box. If the file contents are read into the buffer and then the buffer contents sent to the text box, the text box contents are all messed up. What seems to happen is the newline char is ignored for some reason resulting obviously in a complete loss of any formatting. I remembered something I had read a few years ago about some files having different line ending chars. Clutching at straws I decided to try replacing\n chars with \r\n pairs. My trusty Deitel and Deitel C book states that \r moves to the beginning of the current line and \n moves to the beginning of the next line. For some strange reason this combination of chars worked and the SetDlgItemText() function call worked fine. Getting to the point of this long-winded post :-) I am just wondering if anyone can explain why the \r\n combination is necessary? I am posting the important parts of the code as well in case anyone can spot something there. . . . FILE *inPtr; if((inPtr = fopen("license.txt", "r")) == NULL) return false; // Get file size fseek(inPtr, 0L, SEEK_END); long fileSize = ftell(inPtr); rewind(inPtr); // Create buffer char *txtBuffer = new char[fileSize]; char *destBuffer = new char[fileSize]; // Read the data into the buffer fread(txtBuffer, fileSize, 1, inPtr); // Close the file fclose(inPtr); If you now do this: SetDlgItemText(hwnd, IDC_LICENCE_TEXT, txtBuffer); the textbox gets given a string of rubbish. But, if you instead do something like this to replace \n with \r\n then the textbox gets what was expected. for(long i = 0; i < fileSize; i++) { if(txtBuffer == ''\n'') { destBuffer[destCnt] = ''\r''; destBuffer[destCnt + 1] = ''\n''; destCnt++; } else { destBuffer[destCnt] = txtBuffer[i]; } destCnt++; } SetDlgItemText(hwnd, IDC_LICENCE_TEXT, destBuffer); // Now the textbox is fine Any info would be appreciated!

Share this post

Link to post
Share on other sites
So it''s just another example of Windows oddities :-)
Funnily enough I thought that was the case.
Thanks for the confirmation on that though!

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!