读一个字符串与sscanf的空间 [英] reading a string with spaces with sscanf

查看:96
本文介绍了读一个字符串与sscanf的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的一个项目,我想读一个int和一个字符串的字符串。唯一的问题是sscanf的出现打破读取%S当它看到的空间。反正有绕过这个限制?这里是什么我试图做一个例子:

For a project I'm trying to read an int and a string from a string. The only problem is sscanf appears to break reading an %s when it sees a space. Is there anyway to get around this limitation? Here's an example of what I'm trying to do:

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

int main(int argc, char** argv) {
    int age;
    char* buffer;
    buffer = malloc(200 * sizeof(char));
    sscanf("19 cool kid", "%d %s", &age, buffer);

    printf("%s is %d years old\n", buffer, age);
    return 0;
}

什么它打印的是:酷是19岁我需要酷小子是19岁。有谁知道如何解决这一问题?

What it prints is: "cool is 19 years old" where I need "cool kid is 19 years old". Does anyone know how to fix this?

推荐答案

下面一行将开始阅读一些(%d个),其次是任何东西,从制表符,换行符不同(%[^ \\ t \\ n] )。

The following line will start reading a number (%d) followed by anything different from tabs or newlines (%[^\t\n]).

sscanf("19 cool kid", "%d %[^\t\n]", &age, buffer);

这篇关于读一个字符串与sscanf的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆