簡體   English   中英

為什么sscanf什么都不讀

[英]Why is sscanf not reading anything

所以我試圖使用sscanf讀取字符串,但似乎什么也沒讀。 我遵循了庭審,看起來非常相似。 我無法弄清楚為什么它什么也沒讀。

int main(){

    int status =0;
    int ret = 0;
    int arg;
    char *cmdLine = NULL;
    char *cmd=NULL;
    size_t n = 0;
    char *line = NULL;
    char *token =NULL;

    while (getline(&line, &n, stdin) > 0){
        //toekenize line
        token = strtok(line,";");

        //go thorugh and scan for cmds
        while(token !=NULL){
            // printf("token=%s\n", token);
            cmdLine = token;

            printf("%s\n", cmdLine);
            //read the commands
            ret=sscanf(cmdLine, "%31s %d", cmd, &arg);

            printf("%d\n", ret);

            token = strtok(NULL, ";");
        }//while loop 2
        //set line and n back to null and 0.
        line = NULL;
        n = 0;
    }//while loop 1

嘗試這個:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    int status =0;
    int ret = 0;
    int arg;
    char *cmdLine = NULL;
    char cmd[100];
    size_t n = 0;
    char *line = NULL;
    char *token =NULL;

    if (getline(&line, &n, stdin) > 0)
    {
        //toekenize line
        token = strtok(line,";");

        //go thorugh and scan for cmds
        if(token != NULL)
        {
            // printf("token=%s\n", token);
            cmdLine = token;

            printf(">>>> %s \n", cmdLine);
            //read the commands
            ret = sscanf(cmdLine, "%s %d", cmd, &arg);
            printf(">>>> %d \n", ret);

            token = strtok(NULL, ";");

        }//while loop 2

        //set line and n back to null and 0.
        line = NULL;
        n = 0;
    }//while loop 1

    printf("Result string: %s and Arg: %d \n", cmd, arg);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM