sprintf() without trailing null space in C

2020-05-19 04:20发布

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.

8条回答
不美不萌又怎样
2楼-- · 2020-05-19 04:26

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>';
查看更多
劳资没心,怎么记你
3楼-- · 2020-05-19 04:29

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

查看更多
Deceive 欺骗
4楼-- · 2020-05-19 04:34

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

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

查看更多
家丑人穷心不美
5楼-- · 2020-05-19 04:35

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.

查看更多
Deceive 欺骗
6楼-- · 2020-05-19 04:39

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

查看更多
The star\"
7楼-- · 2020-05-19 04:40

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;
查看更多
登录 后发表回答