为什么 sscanf 不处理或忽略 C++ 中的空格? [英] Why sscanf is not handling or ignoring spaces in c++?

查看:102
本文介绍了为什么 sscanf 不处理或忽略 C++ 中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下行要放在 sscanf 中,但它不接受空格

I have the following line to be put up in sscanf but its not accepting white spaces

char store_string[25],store_string1[10];
std::tm t={};
int store_integer,store_integer1,store_integer2;
int total_read;
           
char* buff1 = "Demo to dispay (Appname,App usage) with date (03/11/2005) with test id (87773) data added (0) total ((null))";
char* buff2 = "Demo to dispay %s with date (%d/%d/%d) with test id (%d) data added (%d) total ((%d))";
            
total_read = sscanf(buff1, buff2 , store_string,  &t.tm_mon,
&t.tm_mday,
&t.tm_year,
&store_integer,&store_integer1,&store_integer2);

   

输出:

如果 buff1 有显示 (Appname,Appusage) 它工作正常

if buff1 has display (Appname,Appusage) it works fine

但是如果 buff1 有显示 (Appname,App usage) 它失败了

But If buff1 has display (Appname,App usage) it fails

推荐答案

为什么 sscanf 不处理或忽略 C++ 中的空格?

Why sscanf is not handling or ignoring spaces in c++?

sscanf 还应该如何分离单词/标记?来自其文档:

How else should sscanf seperate words/tokens? From its documentation:

[%s"] 匹配一系列非空白字符(字符串)

["%s"] matches a sequence of non-whitespace characters (a string)

我认为您无法使用 sscanf 或单行代码来处理此问题.您可以使用 std::string::find_first_ofstd::string::substr

I don't think you can handle this with sscanf or in a one-liner at all. You could do this with std::string::find_first_of and std::string::substr

std::regex_match 可能是最灵活和可读的方式,如果你已经习惯了正则表达式.

std::regex_match might be the most flexible and readable way if you're used to regular expressions.

这篇关于为什么 sscanf 不处理或忽略 C++ 中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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