gpt4 book ai didi

c - 为什么 sscanf 没有读取任何内容

转载 作者:行者123 更新时间:2023-11-30 15:02:30 24 4
gpt4 key购买 nike

所以我尝试使用 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);
}

关于c - 为什么 sscanf 没有读取任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41030229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com