简体   繁体   English

c将可变长度int连接到字符串而不打印它

[英]c concatenate variable length int to a string without printing it

I need to concatenate an integer to a system call string: 我需要将整数连接到系统调用字符串:

status = system("./run test.txt " + integer);

integer here can be any int. integer可以是任何整数。

What is the best way of doing this? 最好的方法是什么?

Use snprintf (or sprintf if you don't have snprintf or need your code to run on systems without it) to print to a char buffer, and pass that to the system call. 使用snprintf (如果没有snprintf或需要代码在没有它的系统上运行,则使用sprintf )打印到char缓冲区,并将其传递给system调用。

eg 例如

#define MAX_LEN 128
char buffer[MAX_LEN];
int val = 0;
snprintf(buffer, MAX_LEN, "./run test.txt %d", val);

// you would be wise to check that snprintf has not truncated your command
// before passing it to system()
status = system(buffer);

Alternatively, you could calculate how many characters the integer needs and then allocate an exactly correctly sized buffer. 或者,您可以计算整数需要多少个字符,然后分配大小正确正确的缓冲区。 This would allow you to use sprintf safely and removes the need to check for truncation - chux's answer demonstrates this. 这将允许您安全地使用sprintf ,而无需检查截断-chux的答案证明了这一点。 Note that this may not be a good strategy if you cannot use VLAs (C89) and have reasons to avoid malloc() , eg on some embedded systems. 请注意,如果您不能使用VLA(C89)并且有避免使用malloc()理由malloc()例如在某些嵌入式系统上malloc() ,那么这可能不是一个好的策略。

After accept answer 接受答案后

Use a right-sized buffer via VLA or malloc() . 通过VLAmalloc()使用大小合适的缓冲区。 Estimating a fixed buffer size may overflow the buffer with sprintf() or create a truncated result with snprintf() . 估计固定的缓冲区大小可能会使sprintf()溢出缓冲区,或者使用snprintf()产生截断的结果。

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

int system_command_int(const char *command, int i) {
  #define INT_PRINT_SIZE(i) ((sizeof(i) * CHAR_BIT)/3 + 3)
  char buf[strlen(command) + 1 + INT_PRINT_SIZE(i) + 1];

  sprintf(buf, "%s %d",command, i);
  return system(buf);
}

int status = system_command_int("./run test.txt", integer);

The INT_PRINT_SIZE(i) macro returns the char size needed to accommodate decimal representation of integer type i . INT_PRINT_SIZE(i)宏返回容纳整数类型i十进制表示形式所需的char大小。 With some integer types, the result may be 1 or 2 extra, but is never too small. 对于某些整数类型,结果可能会额外增加1或2,但永远不会太小。 Alternatively, code could use 10*sizeof(type)*CHAR_BIT/33 + 3 which has less extra overhead. 或者,代码可以使用10*sizeof(type)*CHAR_BIT/33 + 3 ,这会减少额外的开销。 The idea is to multiple the bit width by some integer a/b fraction that is near and >= log10(2) or ~0.30103. 想法是将位宽度乘以接近且> = log10(2)或〜0.30103的某个整数a / b分数。 This size does include the char need for the trailing '\\0' . 此大小确实包含尾随'\\0'char需求。 So the above char buf[] does not need the trailing + 1 . 因此,上面的char buf[]不需要结尾+ 1

Using sprintf : 使用sprintf

char buffer[256];
sprintf(buffer,"./run test.txt %d",integer);
status = system(buffer);

see man sprintf 见男人sprintf

char buf[150];

sprintf(buf, "./run test.txt %d", integer);
status = system(buf);

Make sure buf isn't too small. 确保buf不太小。

#define COMMAND_SIZE 100

char command[COMMAND_SIZE];
sprintf(command, "./run test.txt %d", integer);
status = system(command);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM