printf / scanf系列中的格式%n是否有任何实际应用?

时间:2008-12-09 17:45:30

标签: c printf scanf

int x;
printf("hello %n World\n", &x);
printf("%d\n", x);

8 个答案:

答案 0 :(得分:15)

它对printf()没那么有用,但它对sscanf()非常有用,特别是如果你在多次迭代中解析一个字符串。 fscanf()scanf()会根据读取的输入量自动推进其内部指针,但sscanf()不会。例如:

char stringToParse[256];
...
char *curPosInString = stringToParse;  // start parsing at the beginning
int bytesRead;
while(needsParsing())
{
    sscanf(curPosInString, "(format string)%n", ..., &bytesRead);  // check the return value here
    curPosInString += bytesRead;  // Advance read pointer
    ...
}

答案 1 :(得分:5)

它可用于执行evil deeds

答案 2 :(得分:4)

取决于你的实际意义。总有其他方法可以实现它(例如,使用s [n] printf打印到字符串缓冲区并计算长度。)

然而

int len;
char *thing = "label of unknown length";
char *value = "value value value"
char *value2="second line of value";
printf ("%s other stuff: %n", thing, &len);
printf ("%s\n%*s, value, len, value2);

应该产生

label of unknown length other stuff: value value value
                                     second line of value

(虽然未经测试,我不在C编译器附近)

这是一种实用的对齐方式,但我不想在代码中看到它。有更好的方法。

答案 3 :(得分:3)

这是相当深奥的。如果您需要在生成的字符串中替换占位符,则可能需要记住字符串中间的索引,这样您就不必保存原始printf参数或解析字符串。

答案 4 :(得分:1)

它可以用作获取各种子串的长度的快速方法。

答案 5 :(得分:1)

#include <stdio.h>
int main(int argc, char* argv[])
{
    int col10 = (10 - 1);
    int col25 = (25 - 1);

    int pos1 = 0;
    int pos2 = 0;

    printf("    5    10   15   20   25   30\n");

    printf("%s%n%*s%n%*s\n",                     "fried", 
                            &pos1, col10 - pos1, "green",   
                            &pos2, col25 - pos2, "tomatos");


    printf("    ^    ^    ^    ^    ^    ^\n");

    printf("%d %d\n", pos1, pos2);
    printf("%d %d\n", col10 - pos1, col25 - pos2);

    return 0;
}

我肯定在这里遗漏了一些东西。西红柿离右边太远。

答案 6 :(得分:0)

这是来自VS2005 CRT代码的内容:

/* if %n is disabled, we skip an arg and print 'n' */
if ( !_get_printf_count_output() )
{
   _VALIDATE_RETURN(("'n' format specifier disabled", 0), EINVAL, -1);
   break;
}

提出了这个:

alt text http://www.shiny.co.il/shooshx/printfn.png

以下一行:

    printf ("%s other stuff: %n", thing, &len);

我猜这主要是为了避免@eJames所说的

答案 7 :(得分:0)

你可以打电话

int _get_printf_count_output();

查看%n支持是否已启用,或使用

int _set_printf_count_output( int enable );

启用或禁用%n格式的支持。

来自MSDN VS2008

相关问题