scanf可以識別字符串中的格式字符嗎?

[英]Can scanf identify a format character within a string?


Let's say that I expect a list of items from the standard input which are separated buy commas, like this:

假設我希望標准輸入中的項目列表是分開的購買逗號,如下所示:

 item1, item2, item3,...,itemn

and I also want to permit the user to emit white-spaces between items and commas, so this kind of input is legal in my program:

我還想允許用戶在項目和逗號之間發出空格,所以這種輸入在我的程序中是合法的:

item1,item2,item3,...,itemn

If I use scanf like this:

如果我像這樣使用scanf:

scanf("%s,%s,%s,%s,...,%s", s1, s2, s3, s4,...,sn);

it will fail when there are no white-spaces (I tested it) because it will refer to the whole input as one string. So how can I solve this problem only with C standard library functions?

當沒有空格(我測試過它)時它會失敗,因為它會將整個輸入稱為一個字符串。那么如何才能用C標准庫函數解決這個問題呢?

3 个解决方案

#1


You could have a look at strtok. First read the line into a buffer, then tokenize:

你可以看看strtok。首先將行讀入緩沖區,然后標記:

const int BUFFERSIZE = 32768;
char buffer[BUFFERSIZE];
fgets(buffer, sizeof(buffer), stdin);

const char* delimiters = " ,\n";
char* p = strtok(buffer, delimiters);
while (p != NULL)
{
  printf("%s\n", pch);
  p = strtok(NULL, delimiters);
}

However, with strtok you'll need to be aware of the potential issues related to reentrance.

但是,通過strtok,您需要了解與重入相關的潛在問題。

#2


The quick answer is never, ever use scanf to read user input. It is intended for reading strictly formatted input from files, and even then isn't much good. At the least, you should be reading entire lines and then parsing them with sscanf(), which gives you some chance to correct errors. at best you should be writing your own parsing functions

快速回答永遠不會使用scanf來讀取用戶輸入。它用於從文件中讀取嚴格格式化的輸入,即使這樣也不是很好。至少,您應該讀取整行,然后使用sscanf()解析它們,這樣您就有機會糾正錯誤。最好你應該編寫自己的解析函數

If you are actually using C++, investigate the use of the c++ string and stream classes, which are much more powerful and safe.

如果您實際使用的是C ++,請研究c ++字符串和流類的使用,這些類更強大,更安全。

#3


I guess it is better to write your own parsing function for this. But if you still prefer scanf despite of its pitfalls, you can do some workaround, just substitute %s with %[^, \t\r\n].

我想最好為此編寫自己的解析函數。但是如果你仍然喜歡scanf,盡管存在陷阱,你可以做一些解決方法,只需用%[^,\ t \ r \ n]代替%s。

The problem that %s match sequence of non white space characters, so it swallows comma too. So if you replace %s with %[^, \t\r\n] it will work almost the same (difference is that %s uses isspace(3) to match space characters but in this case you explicitly specify which space characters to match and this list probably not the same as for isspace).

%s匹配非空白字符序列的問題,所以它也吞下逗號。因此,如果用%[^,\ t \ r \ n \ n]替換%s,它將幾乎相同(不同之處在於%s使用isspace(3)來匹配空格字符,但在這種情況下,您明確指定了哪些空格字符匹配和此列表可能與isspace不同)。

Please note, if you want to allow spaces before and after comma you must add white space to your format string. Format string "%[^, \t\r\n] , %[^, \t\r\n]" matches strings like "hello,world", "hello, world", "hello , world".

請注意,如果要在逗號之前和之后允許空格,則必須在格式字符串中添加空格。格式字符串“%[^,\ t \ r \ n],%[^,\ t \ r \ n]”匹配“hello,world”,“hello,world”,“hello,world”等字符串。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/12/72f02a9ce37f589d87a858f68514e90.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com