用 scanf 讀取字串時的空白問題
不過 scanf 有些討厭的地方。一般常見的用法可能是:
scanf 就會將你輸入的資料依照你所設定的「%s」格式丟到變數 data 裡面去。
但是這時候問題就來了,如果你輸入的資料中間含有空白字元時,
例如「my ass」-噹噹,data 的內容就只剩下「my」了。
這當然不是我們所樂見的結果,scanf 的多事造成了我們的困擾...
不過沒關係,根據 google 偷偷告訴我,這症頭可以這樣解:
夠簡單吧!好,打完收工。
參考資料:
char data[10];
printf("Enter some text : ");
scanf("%s", data);
scanf 就會將你輸入的資料依照你所設定的「%s」格式丟到變數 data 裡面去。
但是這時候問題就來了,如果你輸入的資料中間含有空白字元時,
例如「my ass」-噹噹,data 的內容就只剩下「my」了。
這當然不是我們所樂見的結果,scanf 的多事造成了我們的困擾...
不過沒關係,根據 google 偷偷告訴我,這症頭可以這樣解:
scanf("%[^\n]", data);
夠簡單吧!好,打完收工。
參考資料:
c語言學習零碎整理(10):探討scanf函數 - wayne 我的陋室我做主 - CSDNBlog
這篇屁話被收在分類:
寫程式真高興0 則引用
本篇屁話的引用連結 : http://thecrap.org/mt-tb.cgi/382
引用 用 scanf 讀取字串時的空白問題 的文章如下 :

好久不見的scanf啊
I'm viewing your site through IE and not all of the images is displaying correctly. Did you know about this?
Yes, it shows what we need to know.
An excellent article many thanks
Am I the only one to disagree?
Thank you for your help!
Many thanks Wow! Thank you! I often planned to write during my web page anything at all like that. May i get element of one??s submit to my blog?
I want to start a blog. How do I get free web site traffic?
Is it possible to have CPAlead widget in a Joomla site?
good writing so far ...Bookmarked.
Yes, i am glad that your asking us to know our opinion.
This is the second post I've read on this blog and I'm definitely going to bookmark and check back everyday.
My spouse and I absolutely love your blog and find many of your post's to be what precisely I'm looking for. Would you offer guest writers to write content for you personally? I wouldn't mind writing a post or elaborating on a number of the subjects you write in relation to here. Again, awesome weblog!
Terrific exhausted ! I aspire to trainee even if you change a person's internet site, the way in which could quite possibly that i join up to have a blog site web site? All the bank account aided us a accepted come to terms. I got a touch little bit up to date of that your own put out given light very clear approach
Not so bad !
I completely agree with the above comment, the internet is with a doubt growing into the most important medium of communication across the globe and its due to sites like this that ideas are spreading so quickly.
Hey, great blog you will have here, think I discovered it on Yahoo but i am not sure nowanyway,
I cant consider youre avoid popular when you positively develop the gift.
Everyone loves the brand new design of your websites,it appears excellent! This web site rocks !. I simply bookmarked it and looking out forward to read by a different posts
用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚ I was recommended this website by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are incredible! Thanks! your article about 用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚ Best Regards Justin Schaad
用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚ I was suggested this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my difficulty. You are amazing! Thanks! your article about 用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚ Best Regards Agata Lisa
用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚ I was suggested this website by my cousin. I'm not sure whether this post is written by him as no one else know such detailed about my problem. You're amazing! Thanks! your article about 用 scanf 讀å–å—串時的空白å•題 - æ†‘ä½ çš„æ™ºæ…§ï¼Œæˆ‘å¾ˆé›£è·Ÿä½ è§£é‡‹ã€‚Best Regards Lawrence