Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c将可变长度int连接到字符串,而不打印它_C_String_Int_Concatenation - Fatal编程技术网

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

c将可变长度int连接到字符串,而不打印它,c,string,int,concatenation,C,String,Int,Concatenation,我需要将整数连接到系统调用字符串: status=system(“./run test.txt”+整数) integer这里可以是任何int 执行此操作的最佳方法是什么?使用(或者如果您没有snprintf或需要在没有它的系统上运行代码)打印到字符缓冲区,并将其传递到系统调用 e、 g 或者,您可以计算整数需要多少个字符,然后分配一个大小正确的缓冲区。这将允许您安全地使用sprintf,并且不需要检查截断-演示了这一点。请注意,如果您不能使用VLAs(C89),并且有理由避免使用malloc()

我需要将整数连接到系统调用字符串:

status=system(“./run test.txt”+整数)

integer
这里可以是任何int

执行此操作的最佳方法是什么?

使用(或者如果您没有
snprintf
或需要在没有它的系统上运行代码)打印到字符缓冲区,并将其传递到
系统调用

e、 g

或者,您可以计算整数需要多少个字符,然后分配一个大小正确的缓冲区。这将允许您安全地使用
sprintf
,并且不需要检查截断-演示了这一点。请注意,如果您不能使用VLAs(C89),并且有理由避免使用malloc()
,例如在某些嵌入式系统上,这可能不是一个好的策略。

使用:

见曼斯普林特

char buf[150];

sprintf(buf, "./run test.txt %d", integer);
status = system(buf);
确保buf不是太小。

接受回答后

#define COMMAND_SIZE 100

char command[COMMAND_SIZE];
sprintf(command, "./run test.txt %d", integer);
status = system(command);
通过或
malloc()
使用大小合适的缓冲区。估计固定的缓冲区大小可能会使用
sprintf()
使缓冲区溢出,或者使用
snprintf()
创建截断结果

#包括
#包括
#包括
#包括
int system_command_int(常量字符*命令,int i){
#定义整数打印大小(i)((i)*字符位)/3+3)
字符大小[strlen(命令)+1+整数打印大小(i)+1];
sprintf(buf,“%s%d”,命令,i);
返回系统(buf);
}
int status=系统命令int(“./run test.txt”,整数);


INT\u PRINT\u SIZE(i)
宏返回容纳整数类型
i
的十进制表示所需的
char
大小。对于某些整数类型,结果可能是额外的1或2,但永远不会太小。或者,代码可以使用额外开销较小的
10*sizeof(type)*字符位/33+3
。其思想是将比特宽度乘以某个接近且>=log10(2)或~0.30103的整数a/b分数。此大小不包括尾随
'\0'
所需的
字符。因此,上面的
char buf[]
不需要“不打印”后面的
+1

您的意思是
sprintf()
/
snprintf()
函数不能使用吗?如果是这样,为什么?不,对不起,只是对C来说很陌生,通过这些函数的命名,我想为什么它们会将输出打印到屏幕上。
sprintf
打印到一个“字符串”(
char*
,真的)<默认情况下,code>printf
打印到
stdout
,而
fprintf
打印到
文件*
。缓冲区的长度在这里重要吗?为什么要使用256?缓冲区长度需要足够长。如果您有snprintf,并且不需要担心可移植性,请使用它。@YemSalat:它需要足够长的时间来执行您的命令,以及可能适合的最长int。缓冲区需要足够大,256只是一些任意的“足够大”值。在这种情况下,可以认为int最多占用11个字符(假设32位int),因此字符串(15)+11+1的固定部分的长度足以容纳终止\0=27。@IskarJarak建议使用更安全的snprintf函数来限制其输出的长度,这使得该数学变得过时。@fvu使用
snprintf()
来简单地限制打印到缓冲区的字符是不够的。使用截断的缓冲区调用系统函数是一个主要问题。使用
snprintf()
sprint()
时需要额外的代码,以防止使用太小的缓冲区。为什么这里的命令大小=100?@YemSalat,这是使用
snprintf
捕获
命令的最大长度的一种方法@R Sahu只稍微安全一点,因为它没有溢出
缓冲区。使用
snprintf()
会导致另一个问题:执行截断的命令。检测缓冲区可能溢出并且不执行系统命令比执行截断的命令要好得多。这并不是在使用构造命令字符串时如何安全调用system()的完整示例,只是如何(安全地)调用system()的示例将一个字符串和一个整数连接到一个缓冲区,并向缓冲区传递一个函数,但我已经添加了一个关于前者的注释。同时考虑到OP的情况,他应该能够预先计算命令的最大长度。Iskar Jarak如果命令长度是check a priori,则需要
snprintf()
(vs.
sprintf()
)可以被否定。您的答案并没有吹嘘
snprintf()
优于
sprintf()
,而是@R Sahu提升了
snprintf()
,但忽略了截断问题。为了解决这个问题,
snprintf()
并不比
printf()
便宜多少。像
int
overflow一样,最好先检测溢出。@chux我的观点只是我认为问题是关于连接的,所以教OP关于
snprintf
是相关的。
char buf[150];

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

char command[COMMAND_SIZE];
sprintf(command, "./run test.txt %d", integer);
status = system(command);
#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);