简体   繁体   English

卡在myPrintf和va_arg列表中

[英]Stuck with myPrintf and va_arg list

i'm currently working on my own implementation of scanf function, but i cannot use any library functions except for va_ macros. 我目前正在使用我自己的scanf函数实现,但是除va_宏外,我无法使用任何库函数。

Everything pretty much works except for printing stuff that is in *fmt variable and is not a %d, %s etc. 除了打印* fmt变量中的内容(不是%d,%s等)之外,其他所有内容都可以正常工作。

For example myPrintf("%d stuff and more stuff %s",50,"foo"); 例如myPrintf(“%d的东西和更多的东西%s”,50,“ foo”);

will print only 50foo and not 50 stuff and more stuff foo 将只打印50foo,而不是50和更多的东西foo

I tried messing around with default part of swtich but it led me to nothing Thank you in advance! 我试着弄乱了swtich的默认部分,但是这让我无所适从,谢谢!

void myPrintf(char *fmt, ...)
{
    va_list ap;
    char *s;
    char buf[64];
    va_start(ap, fmt);

    while(*fmt)
    {
        x++;
        switch(*fmt++)
        {
        case 's':
            s = va_arg(ap, char*);
            write(1,s,strlen(s));
            break;
        case 'd':
            s = toString(va_arg(ap, int));
            write(1,s,strlen(s));
            break;
        case 'b':
            s = toBin(va_arg(ap, int));
            write(1,s,strlen(s));
            break;
        default:
            break;
        }
    va_end(ap);
   }

}

It prints 50foo because it only prints when it saw "d" or "s" or "b" in fmt. 它之所以打印50foo,是因为只有在fmt中看到“ d”,“ s”或“ b”时才打印。 You should check for "%" to print the arguments you give, and print characters from fmt otherwise. 您应该检查“%”以打印您提供的参数,否则打印来自fmt的字符。 It should be something like the code below: 它应该类似于以下代码:

void myPrintf(char *fmt, ...)
{
    va_list ap;
    char *s;
    char c;
    char s2[100];
    char buf[64];
    va_start(ap, fmt);

    while(*fmt)
    {
        c = *fmt;
        if (c == '%') {
            fmt++;
            switch(*fmt)
            {
            case 's':
                s = va_arg(ap, char *);
                write(1, s, strlen(s));
                break;
            case 'd':
                memset(s2, 0, 100);
                sprintf(s2,"%d",va_arg(ap, int));
                write(1, s2, strlen(s2));
                break;
            default:
                break;
            }
        } else {
            write(1, fmt, 1);
        }
        va_end(ap);
        fmt++;
    }
    write(1, 0, 1);
}

Well, I edited your code a litte to make it work on my machine, since i don't have the implementations of toString() and toBin(). 好吧,由于我没有toString()和toBin()的实现,因此我对您的代码进行了少量编辑以使其在我的机器上工作。 But, the implementation above will show you where you got it wrong. 但是,上面的实现将向您显示错误的地方。 The code is checking for "%" symbol first, and when it saw "%" then it checks for "%s" and "%d" to print the arguments '50' and 'foo'. 该代码首先检查“%”符号,当它看到“%”时,则检查“%s”和“%d”以打印参数“ 50”和“ foo”。 In all other cases, it prints characters from fmt, so 'stuff and more stuff' is also printed to stdin. 在所有其他情况下,它将打印来自fmt的字符,因此还将“ stuff and more stuff”打印到stdin。

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

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