Left padding string - sprintf() woes.

I’m having a rough time padding a string using sprintf(). When my arduino hits the code lines to call sprintf(), it simply stops/stalls from what I can tell.

I’m newish to C++, but not to programming in general, though haven’t used sprintf() much before.

I think that any of these should left-pad the string, but they don’t work - I end up with nothing on screen.

sprintf(thisFixeLenLineCharBuffer, "%*s%s", 20, thisLineString);
sprintf(thisFixeLenLineCharBuffer, "%-20s", thisLineString);
sprintf(thisFixeLenLineCharBuffer, "%-*s", 20, thisLineString);

Here’s the basic code for the function I’m trying to run that will print on the LCD

void printPaddedFixedLenMsgToLCD(String msg){
	//This function expects a char string of length LCD_ROWS * LCD_COLUMNS (ex 32 for 2x16 LCD)
	//    that will be split every LCD_COLUMNS chars (ex 0-15 and 16-31) and written to the LCD
	static char thisFixeLenLineCharBuffer[LCD_COLUMNS];
	String thisLineString = "";
	
	Serial.println("printPaddedFixedLenMsgToLCD called");
	
	// Wipe the LCD screen
	lcd.clear();

	for (int i = 0; i < LCD_ROWS; i++) {

		//start writing to line 1
		lcd.setCursor(0,i);
		//Strip out the section for this line
		thisLineString = msg.substring(i*LCD_COLUMNS,(i+1)*LCD_COLUMNS);
		
		//lcd.print(msg.substring(i*LCD_COLUMNS,(i+1)*LCD_COLUMNS-1));
		sprintf(thisFixeLenLineCharBuffer, "%*s%s", 20, thisLineString);
		
		Serial.print("Printing={");
		//Serial.println((String) thisFixeLenLineCharBuffer);
		Serial.print(thisLineString);
		Serial.println("}");
		
		lcd.print( (String) thisFixeLenLineCharBuffer );
	}
}
sprintf(thisFixeLenLineCharBuffer, "%*s%s", 20, thisLineString);

sprintf() doesn't know anything about Strings. Quit using them. Everything that a String can do, a string can, too, with much less resource waste.

PaulS:

sprintf(thisFixeLenLineCharBuffer, "%*s%s", 20, thisLineString);

sprintf() doesn't know anything about Strings. Quit using them. Everything that a String can do, a string can, too, with much less resource waste.

@PaulS - Are you saying it's an issue with the String object or the sprintf() function? Your statement may be a great overall programming suggestion, but doesn't seem very helpful (or specific) with my original question about sprintf().

nullRequest:
Your statement may be a great overall programming suggestion, but doesn't seem very helpful (or specific) with my original question about sprintf().

I don't understand why you say that.

You are reporting a problem with your use of sprintf().

PaulS informs you that one of the arguments you're passing to this function is of the wrong type - you are passing in one of those cursed String objects where the function expects a char*. It's an unfortunate limitation of the varargs type interface to printf() et al that this sort of error cannot be detected by the compiler. Fortunately for you, PaulS has spotted it.

It seems to me that this is probably the cause of your problem, and at the very least should be corrected before you go any further.

There are nicer more helpful ways to state things…
having said that I was having a similar issue with sprintf.

I think that sprintf on arduino does not support the width format for strings just as it does not support format strings for floats so even if he had specified a string over a String it wouldn’t have worked either.

There seems to be a lot of hate for anything that resembles OO programming in C forums.

There seems to be a lot of hate for anything that resembles OO programming in C forums.

That's a bit of a non sequitur, IMO (this isn't a C forum), but there is a movement to steer people away from inappropriate constructs in a RAM-limited architecture.

My testing indicates that the “*” width specifier does not work. Like floats, I suspect that this is to save space in the object code, as such code would have to be unconditionally included.

As for the original question, the OP was simply supplying the wrong type.

dilbert:
There seems to be a lot of hate for anything that resembles OO programming in C forums.

I don’t know about that. Most of the libraries use C++ classes (eg. HardwareSerial, SPI, Wire). There isn’t “hate”. However the String class can be a bit of a memory hog. You only have 2 Kb of memory on a Uno, we are warning you about that. This is “love”. We are trying to help.

Well I found documentation. :slight_smile:

In my case it was at:

/Contents/Resources/Java/hardware/tools/avr/doc/avr-libc/group__avr__stdio.html

The variable width or precision field (an asterisk * symbol) is not realized and will to abort the output.

		sprintf(thisFixeLenLineCharBuffer, "%*s%s", 20, thisLineString);

Why not:

		sprintf(thisFixeLenLineCharBuffer, "%20s", thisLineString);

The extra %s was wrong in that code in any case.