0

I am working on Mac OSX and using bash as my shell. I am working in C and I am trying to create a file that will renumber files. The important part of my code is as follows:

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.
}

This gets me the correct string format I am looking for (say formatLength=4): filePrefix+%04d. However, now I need to evaluate the %04d in the string and evaluate it as i, so that the files look like: file0001, file0002, etc.

Would anyone have any ideas. Thanks for your help.

4

5 回答 5

3

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

int formatLength = /* some input */;
char filePrefix[FILEPREFIX_LEN]; // assigned by some input
const int FILENAME_LEN = strlen(filePrefix) + formatLength + 1; // +1 for terminating '\0'
char fileName[FILENAME_LEN];

int i;
for (i=0; i<numberOfFiles; i++) {
    char temp[TEMPLATE_LEN]; // where TEMPLATE_LEN >= FILEPREFIX_LEN + 3 + number of characters in the decimal representation of formatLength
    snprintf(temp, TEMPLATE_LEN, "%s%%0%dd", filePrefix, formatLength);
    // error check snprintf here, in case the destination buffer was not large enough
    snprintf(fileName, FILENAME_LEN, temp, i);
    // error check snprintf here, in case the destination buffer was not large enough

    // use fileName
}

因此,如果你的filePrefix= "file" 那么你会得到fileName= "file0001"、"file0002"、"file0003" 等等......

尽管很多这项工作实际上并不依赖于,i因此您可以将其移到循环之外,如下所示:

int formatLength = /* some input */;
char filePrefix[FILEPREFIX_LEN]; // assigned by some input
const int FILENAME_LEN = strlen(filePrefix) + formatLength + 1; // +1 for terminating '\0'
char fileName[FILENAME_LEN];

char temp[TEMPLATE_LEN]; // where TEMPLATE_LEN >= FILEPREFIX_LEN + 3 + number of characters in the decimal representation of formatLength
snprintf(temp, TEMPLATE_LEN, "%s%%0%dd", filePrefix, formatLength);
// error check snprintf here, in case the destination buffer was not large enough

int i;
for (i=0; i<numberOfFiles; i++) {
    snprintf(fileName, FILENAME_LEN, temp, i);
    // error check snprintf here, in case the destination buffer was not large enough

    // use fileName
}

在这些情况下,您的temp(“模板”的缩写,而不是“临时”)将是“prefix%04d”(例如,prefixLength 为 4,filePrefix 为“prefix”)。因此,您需要注意您的filePrefix不包含任何printf对函数族具有特殊意义的字符。如果您先验地知道它不会,那么您就可以开始了。

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

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

%请注意第一个开头的额外内容snprintf()。这使得模板模式为“%s%04d”(例如,prefixLength 为 4),然后在第二次调用中添加 filePrefix,这样它的内容就不是第二次调用中模式字符串的一部分。

于 2013-07-23T18:46:09.373 回答
1

如果我正确理解你的问题,你应该可以说

char result[(sizeof filePrefix/sizeof (char)) + formatLength];
sprintf(result, fileName, i);

因为fileName看起来像"filePrefix%04d". 然后,您想要的文件名将存储在result. fileName我不建议重新存储它,sprintf(fileName, fileName, i)因为它fileName可能太小(例如 when formatLength = 9)。

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

于 2013-07-23T18:30:42.517 回答
1

您可以构建一个格式字符串,然后将其用作另一个格式化程序调用的格式字符串。请注意,前缀和数字格式说明符可以内置到单个字符串中 - 不需要 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.

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

于 2013-07-23T19:08:18.770 回答
0

你快到了

// 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
于 2013-07-23T19:03:51.870 回答
0

sprintf(文件名字符串,文件名,我);// 我认为你的意思是,但使用 snprintf()

于 2013-07-23T18:39:50.750 回答