RajSanpui
RajSanpui

Reputation: 12094

C: snprintf not accepting \n for GUI display. Need suggestions

I need to put the last valid character as NUL, '\0'.

There is a char array char cursStr[128]; My intention is: cursStr [lastCharacter where ends] = '\0'

Here is the code snippet of how, this array is getting populated:

snprintf(num_range, sizeof(num_range), "%d-%d", devIndex, devIndex+127);
snprintf(dev_range, sizeof(dev_range), "%s%d...%s%d", devices[devPointer].name, 1, devices[devPointer].name, 128);
sprintf(cursStr, "%-7s  %-25.50s  %c%-30.30s  %5Ld%11s\n",
            num_range, dev_range,' ', "Empty", (var64)0, "GPT");

The reason why i am doing this, is because by printing in the GUI it is showing some junk caracter at the end. But printing at console prints just fine. I even tried to do a memset of 0 to this array, but it does not help.

This is how it is getting printed to GUI and console.

             if (cursLine) {
                    TVdisplayText(cursLine, cursStr, &usedLines); // GUI
                    cursLine += usedLines;
             } else {
                    printf("%s\n",cursStr); // console
             }

The GUI library used is TVision (Turbo Vision)

Upvotes: 1

Views: 148

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409266

Many GUI systems can not show special characters like newline correctly. Either don't add the newline in the sprintf call, or manually remove it:

/* Remove the last character from the string */
cursStr[strlen(cursStr) - 1] = '\0';

Upvotes: 3

Related Questions