简体   繁体   English

C:如何让sscanf在字符串前保留空格?

[英]C: how to allow sscanf to keep white spaces in front of string?

Hey guys so I am trying to do the following: 嘿家伙所以我试图做以下事情:

I have a string of the form and the _ are actually spaces.: 我有一个表格的字符串,_实际上是空格:

__ 1 _ __ _Multithreaded Programming :: Improving Performance through Threads __ 1 _ __ _Multithreaded Programming ::通过线程提高性能

I want to read the integer without spaces, and then read the remainder of the string including the space that will be in front of the string. 我想读取没有空格的整数,然后读取字符串的剩余部分, 包括将在字符串前面的空格。

I have the following code: 我有以下代码:

while(fgets(line, MAX_STRINGS, stdin)){
    sscanf(line, "%d %88[^\n]s", &num, string_value);
    printf("%d + %s\n", num, string_value);
}

It reads the integer and also it reads the rest of the string ( My requirements for the problem state that the whole string including the integer will not be more than 88 bytes) BUT it ommits all the spaces in front of the string and I need them. 它读取整数并且它还读取字符串的其余部分(我对问题的要求是整个字符串包括整数不超过88个字节)但是它忽略了字符串前面的所有空格而我需要它们。 How can I keep the white spaces? 我怎样才能保留白色空间? Thanks! 谢谢!

Remove the space between %d and %88. 删除%d和%88之间的空格。 A space in the sscanf format string means "consume and discard all whitespace". sscanf格式字符串中的空格表示“使用并丢弃所有空格”。

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

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