Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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中计算字符串中的类型标识符(%d)_C_String_Append - Fatal编程技术网

在C中计算字符串中的类型标识符(%d)

在C中计算字符串中的类型标识符(%d),c,string,append,C,String,Append,我正在MacOSX上工作,并使用bash作为shell。我在用C语言工作,我正在尝试创建一个文件,将文件重新编号。我的代码的重要部分如下: int i; for (i=0; i<numberOfFiles; i++) { strcpy(fileName,""); //Set to Null char append[formatLength]; //String being appended sprintf(append,"%%0%dd", formatLength)

我正在MacOSX上工作,并使用bash作为shell。我在用C语言工作,我正在尝试创建一个文件,将文件重新编号。我的代码的重要部分如下:

int i;
for (i=0; i<numberOfFiles; i++) {
    strcpy(fileName,""); //Set to Null
    char append[formatLength]; //String being appended
    sprintf(append,"%%0%dd", formatLength); //example output: %04d
    strcat(fileName,filePrefix); //Attached Prefix
    strcat(fileName,append); //Attaches appended part

   //Missing code: Part which equvaluates %04d as int i, such as 0023.
}
inti;

因为(i=0;i如果我正确理解了你的问题,你应该能够说

char result[(sizeof filePrefix/sizeof (char)) + formatLength];
sprintf(result, fileName, i);
由于
文件名
看起来像
“filePrefix%04d”
。您所需的文件名将存储在
结果
中。我不建议通过说
sprintf(fileName,fileName,I)
将其重新存储在
文件名
中,因为
文件名
可能太小(例如,当
formatLength=9
时)


请注意,您需要
(sizeof filePrefix/sizeof(char))
来查找
filePrefix
(也可能是
char*
)的大小,然后添加
formatLength
,以查看在此之后您还需要多少char

如果我正确理解了您的问题,您应该能够说:

char result[(sizeof filePrefix/sizeof (char)) + formatLength];
sprintf(result, fileName, i);
由于
文件名
看起来像
“filePrefix%04d”
。您所需的文件名将存储在
结果
中。我不建议通过说
sprintf(fileName,fileName,I)
将其重新存储在
文件名
中,因为
文件名
可能太小(例如,当
formatLength=9
时)


请注意,您需要
(sizeof filePrefix/sizeof(char))
来查找
filePrefix
(这可能也是
char*
)的大小,然后添加
formatLength
,以查看在这之后您需要多少个char

sprintf(filenamesting,fileName,i);//我想您是这个意思,但请使用snprintf()

sprintf(fileNameString,fileName,i);//我想你是这个意思,但是使用snprintf()

使用你用
snprintf()
创建的字符串作为下次调用
snprintf()
的格式字符串

在这些情况下,您的
temp
(缩写为“模板”,而不是“临时”)将是“前缀%04d”(例如,前缀长度为4,文件前缀为“前缀”)。那么,您需要注意的是,您的
文件前缀
不包含任何对
printf
函数族有特殊意义的字符。如果您事先知道它不会,那么您可以选择

但是,如果可能的话,那么您需要做两件事中的一件。您可以在使用
文件前缀之前通过转义所有特殊字符来处理它。或者您可以将
snprintf()
调用更改为类似以下内容:

snprintf(temp, TEMPLATE_LEN, "%%s%%0%dd", formatLength);
// other stuff...
snprintf(fileName, FILENAME_LEN, temp, filePrefix, formatLength);

请注意,在第一个
snprintf()
的开头有一个额外的
%
。这将生成模板模式“%s%04d”(例如,前缀长度为4),然后在第二个调用中添加filePrefix,使其内容不属于第二个调用中的模式字符串。

使用您用
snprintf()创建的字符串
作为下一次调用
snprintf()
的格式字符串

在这些情况下,您的
temp
(缩写为“模板”,而不是“临时”)将是“前缀%04d”(例如,前缀长度为4,文件前缀为“前缀”)。那么,您需要注意的是,您的
文件前缀
不包含任何对
printf
函数族有特殊意义的字符。如果您事先知道它不会,那么您可以选择

但是,如果可能的话,那么您需要做两件事中的一件。您可以在使用
文件前缀之前通过转义所有特殊字符来处理它。或者您可以将
snprintf()
调用更改为类似以下内容:

snprintf(temp, TEMPLATE_LEN, "%%s%%0%dd", formatLength);
// other stuff...
snprintf(fileName, FILENAME_LEN, temp, filePrefix, formatLength);
请注意,在第一个
snprintf()
的开头有一个额外的
%
。这将生成模板模式“%s%04d”(例如,前缀长度为4),然后在第二个调用中添加filePrefix,以便它的内容在第二个调用中不属于模式字符串的一部分。

您就快到了

// This line could be done before the loop
sprintf(append,"%%0%dd", formatLength); //example output: %04d

// Location to store number
char NumBuffer[20];
// Form textual version of number
sprintf(NumBuffer, append, i);
strcat(fileName,filePrefix); //Attached Prefix
strcat(fileName,NumBuffer); //Attaches appended part
你就快到了

// This line could be done before the loop
sprintf(append,"%%0%dd", formatLength); //example output: %04d

// Location to store number
char NumBuffer[20];
// Form textual version of number
sprintf(NumBuffer, append, i);
strcat(fileName,filePrefix); //Attached Prefix
strcat(fileName,NumBuffer); //Attaches appended part

您可以构建一个格式字符串,然后将其用作另一个格式化程序调用的格式字符串。请注意,前缀和数字格式说明符可以构建到单个字符串中,无需strcat调用

鉴于:

char format_specifier[256] ;
然后,可以将示例中的循环代码替换为:

snprintf( format_specifier,
          sizeof( format_specifier),
          "%s%%0%dd",
          filePrefix, 
          formatLength ) ; // Create format string "<filePrefix>%0<formatLength>", 
                           // eg. "file%04d"

snprintf( fileName,         // Where the filename will be built
          sizeof(fileName), // The length of the filename buffer
          format_specifier, // The previously built format string
          i ) ;             // The file number.
snprintf(格式指定符,
sizeof(格式说明符),
“%s%%0%dd”,
文件前缀,
formatLength);//创建格式字符串“%0”,
//例如,“文件%04d”
snprintf(fileName,//将在其中生成文件名
sizeof(fileName),//文件名缓冲区的长度
格式\说明符,//以前生成的格式字符串
i);//文件号。

我在上面假设
fileName
是一个数组,如果它是指向数组的指针,那么sizeof(fileName)将不正确。当然,如果您选择使用
sprintf
而不是
snprintf
,这是学术性的。

您可以构建一个格式字符串,然后将其用作另一个格式化程序调用的格式字符串。请注意,前缀和数字格式说明符可以构建为单个字符串-无需strcat调用

鉴于:

char format_specifier[256] ;
然后,可以将示例中的循环代码替换为:

snprintf( format_specifier,
          sizeof( format_specifier),
          "%s%%0%dd",
          filePrefix, 
          formatLength ) ; // Create format string "<filePrefix>%0<formatLength>", 
                           // eg. "file%04d"

snprintf( fileName,         // Where the filename will be built
          sizeof(fileName), // The length of the filename buffer
          format_specifier, // The previously built format string
          i ) ;             // The file number.
snprintf(格式指定符,
sizeof(格式说明符),
“%s%%0%dd”,
文件前缀,
formatLength);//创建格式字符串“%0”,
//例如,“文件%04d”
snprintf(fileName,//将在其中生成文件名
sizeof(fileName),//文件名缓冲区的长度
格式\说明符,//以前生成的格式字符串
i);//文件号。
我在上面假设
fileName
是一个数组,如果它是指向数组的指针,那么sizeof(fileName)将不正确