用 scanf 讀取字串時的空白問題

| | 迴響 (22) | 引用 (0)


在純 C 的環境下,讀取字串並分存到不同變數時,經常會使用到 scanf...


不過 scanf 有些討厭的地方。一般常見的用法可能是:

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 讀取字串時的空白問題 的文章如下 :

22 則迴響

Eddy 說:

好久不見的scanf啊

I'm viewing your site through IE and not all of the images is displaying correctly. Did you know about this?

Diana Dickun 說:

Yes, it shows what we need to know.

Jaime Urata 說:

An excellent article many thanks

Am I the only one to disagree?

Thank you for your help!

beijing tour 說:

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?

health 說:

I want to start a blog. How do I get free web site traffic?

John Timone 說:

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.

Shayne Regler 說:

This is the second post I've read on this blog and I'm definitely going to bookmark and check back everyday.

Val Morenz 說:

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

Kori Wallace 說:

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.

cheap uggs 說:

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.

ugg boots uk 說:

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

給我一點 tempo

About

這篇屁話是由 Alvin Wu 發表於 April 18, 2008 2:55 PM

上一篇是 六一倒了

下一篇是 Spring Scream 2008

首頁 可以找到最近的屁話,或是到 彙整 去看全部的屁話紀綠

Powered by Movable Type 4.34-en