English 中文(简体)
阅读带有扫描仪的空间体
原标题:Reading a string with spaces with sscanf

“Im”项目试图从地狱中读到一根暗中。 唯一的问题是sscanf()。 当它看到一个空间时。 能否绕过这一限制? 下面是我试图做的事情的一个例子:

#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
", buffer, age);
    return 0;
}

它的印本是:cool是19岁<>/code>,其中我需要cool kid是19岁<>。 是否有任何人知道如何解决这一问题?

最佳回答

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

sscanf("19 cool kid", "%d %[^	
]", &age, buffer);
问题回答

您希望%c换算光谱仪,该图像仅读出一系列特性,而无需专门处理白色空间。

请注意,您首先需要填满零件,因为<代码>%cspecifier don t 写上绝核装置。 你们还需要具体说明读写的特性(否则,只剩下1个):

memset(buffer, 0, 200);
sscanf("19 cool kid", "%d %199c", &age, buffer);

如果你想设法结束扼杀(如果有的话,会铺一条新线),那么就应当:

char *x = "19 cool kid";
sscanf (x, "%d %[^
]", &age, buffer);

That s because %s only matches non-whitespace characters and will stop on the first whitespace it finds. The %[^ ] format specifier will match every character that s not (because of ^) in the selection given (which is a newline). In other words, it will match any other character.


铭记你本应在你的缓冲中分配足够的空间来进行扼杀,因为你无法确保读出多少东西(远离<条码>扫描f/fscanf的一个良好理由)。 除非你使用特定领域的宽度。

你可以这样做:

char *x = "19 cool kid";
char *buffer = malloc (strlen (x) + 1);
sscanf (x, "%d %[^
]", &age, buffer);

always 1 按定义。

由于你想要从投入中提取线索,你可以使用<条码>%<>>>>>(迄今为止所消耗的特性编号)来获得显示线索开始的位置。 这避免了记忆拷贝和缓冲问题,但是,如果你想要一份副本,你可能需要付出代价。

const char *input = "19  cool kid";
int age;
int nameStart = 0;
sscanf(input, "%d %n", &age, &nameStart);
printf("%s is %d years old
", input + nameStart, age);

产出:

cool kid is 19 years old

我猜测这是你所希望的,确实是你所指明的。

#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 cool %s", &age, buffer);
    printf("cool %s is %d years old
", buffer, age);
    return 0;
}

The format expects: first a number (and puts it at where &age points to), then whitespace (zero or more), then the literal string "cool", then whitespace (zero or more) again, and then finally a string (and put that at whatever buffer points to). You forgot the "cool" part in your format string, so the format then just assumes that is the string you were wanting to assign to buffer. But you don t want to assign that string, only skip it.

Alternative, you could also have a format string like: "%d %s %s", but then you must assign another buffer for it (with a different name), and print it as: "%s %s is %d years old ".





相关问题
Simple JAVA: Password Verifier problem

I have a simple problem that says: A password for xyz corporation is supposed to be 6 characters long and made up of a combination of letters and digits. Write a program fragment to read in a string ...

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

String initialization with pair of iterators

I m trying to initialize string with iterators and something like this works: ifstream fin("tmp.txt"); istream_iterator<char> in_i(fin), eos; //here eos is 1 over the end string s(in_i, ...

break a string in parts

I have a string "pc1|pc2|pc3|" I want to get each word on different line like: pc1 pc2 pc3 I need to do this in C#... any suggestions??

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签