SetDlgItemText() Oddity
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;
}
destCnt++;
}
SetDlgItemText(hwnd, IDC_LICENCE_TEXT, destBuffer); // Now the textbox is fine
Any info would be appreciated!
</i>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement