zaratustra
zaratustra

Reputation: 8758

sprintf() without trailing null space in C

Is there a way to use the C sprintf() function without it adding a '\0' character at the end of its output? I need to write formatted text in the middle of a fixed width string.

Upvotes: 55

Views: 109160

Answers (8)

afk
afk

Reputation: 366

Here's an option for memory constrained devices. It trades off speed for using less RAM. I sometimes have to do this to update the middle of a string that gets printed to a LCD.

The idea is that you first call snprintf with a zero sized buffer to determine which index will get clobbered by the null terminator.

You can run the below code here: https://rextester.com/AMOOC49082

#include <stdio.h>
#include <string.h>

int main(void)
{
  char buf[100] = { 'a', 'b', 'c', 'd', 'e' };
  const size_t buf_size = sizeof(buf);
  const int i = 123;

  int result = snprintf(buf, 0, "%i", i);
  if (result < 0)
  {
    printf("snprintf error: %i\n", result);
    return -1;
  }

  int clobbered_index = result; //this index will get the null term written into it

  if (result >= buf_size)
  {
    printf("buffer not large enough. required %i chars\n", result + 1);
    return -1;
  }

  char temp_char = buf[clobbered_index];
  result = snprintf(buf, buf_size, "%i", i); //add result error checking here to catch future mistakes
  buf[clobbered_index] = temp_char;

  printf("buf:%s\n", buf);

  return 0;
}

Prints buf:123de

Upvotes: 1

yan bellavance
yan bellavance

Reputation: 4840

Actually this example will not add a null if you use snprintf:

char name[9] = "QQ40dude";  
unsigned int i0To100 = 63;  
_snprintf(&name[2],2,"%d",i0To100);  
printf(name);// output will be: QQ63dude  

Upvotes: 0

Todd Freed
Todd Freed

Reputation: 933

Since you're writing to a fixed area, you can do it like this:

// pointer to fixed area we want to write to
char* s;

// number of bytes needed, not including the null
int r = snprintf(0, 0, <your va_args here>);

// char following the last char we will write - null goes here
char c = s[r + 1];

// do the formatted write
snprintf(s, r + 1, <your_va_args here>);

// replace what was overwritten
s[r + 1] = c;

Upvotes: 1

nir
nir

Reputation: 159

look here: http://en.wikipedia.org/wiki/Printf

printf("%.*s", 3, "abcdef") will result in "abc" being printed

Upvotes: -4

Roddy
Roddy

Reputation: 68054

You can't do this with sprintf(), but you may be able to with snprintf(), depending on your platform.

You need to know how many characters you are replacing (but as you're putting them into the middle of a string, you probably know that anyway).

This works because some implementations of snprintf() do NOT guarantee that a terminating character is written - presumably for compatibility with functions like stncpy().

char message[32] = "Hello 123, it's good to see you.";

snprintf(&message[6],3,"Joe");

After this, "123" is replaced with "Joe".

On implementations where snprintf() guarantees null termination even if the string is truncated, this won't work. So if code portability is a concern, you should avoid this.

Most Windows-based versions of snprintf() exhibit this behaviour.

But, MacOS and BSD (and maybe linux) appear to always null-terminate.

Upvotes: 9

Nathan Fellman
Nathan Fellman

Reputation: 127538

You could also use your fixed width string as a format string like this:

char my_fixed_width_string_format[] = "need 10 chars starting here: %10s";
char my_fixed_width_string[40];
char string_to_print[] = "abcdefghijklmnop";
sprintf(my_fixed_width_string, my_fixed_width_string_format, string_to_print;
printf(my_fixed_width_string);

should yield

need 10 chars starting here: abcdefghij

Upvotes: 2

Doug T.
Doug T.

Reputation: 65639

sprintf returns the length of the string written (not including the null terminal), you could use that to know where the null terminal was, and change the null terminal character to something else (ie a space). That would be more efficient than using strncpy.

 unsigned int len = sprintf(str, ...);
 str[len] = '<your char here>';

Upvotes: 33

Greg Hewgill
Greg Hewgill

Reputation: 993951

There is no way to tell sprintf() not to write a trailing null. What you can do is use sprintf() to write to a temporary string, and then something like strncpy() to copy only the bytes that you want.

Upvotes: 56

Related Questions