<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>程式設計 遇上 小提琴 &#187; M$</title>
	<atom:link href="http://blog.ez2learn.com/tag/m/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ez2learn.com</link>
	<description>Victor&#039;s個人部落格，關於程式設計與小提琴</description>
	<lastBuildDate>Tue, 07 Feb 2012 03:26:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>不守標準出了名的M$</title>
		<link>http://blog.ez2learn.com/2008/09/29/microsoft-never-follows-the-standard/</link>
		<comments>http://blog.ez2learn.com/2008/09/29/microsoft-never-follows-the-standard/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 16:55:38 +0000</pubDate>
		<dc:creator>victor</dc:creator>
				<category><![CDATA[中文文章]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[M$]]></category>
		<category><![CDATA[MSDN]]></category>
		<category><![CDATA[undefined behavior]]></category>
		<category><![CDATA[標準]]></category>

		<guid isPermaLink="false">http://blog.ez2learn.com/?p=33</guid>
		<description><![CDATA[問題 今天有人問了這樣一個問題，如何不讓scanf取得指定長度後剩下的字元，被下一個scanf取得 解法 首先我想到的是scanf一個字串，可是如果字串長度剛好，就會造成要再輸入一次，於是我又想到了fflush來清空stdin的緩衝區，查了MSDN，也寫了一個小程式來測試，果然可行，但是&#8230; 但是 但是有人說fflush(stdin)不合C語言標準，起先我懷疑真的是這樣嗎? 因為我查MSDN是這樣寫沒錯阿，還有範例示範呢 /* FFLUSH.C */ &#160; #include #include &#160; void main&#40; void &#41; &#123; int integer; char string&#91;81&#93;; &#160; /* Read each word as a string. */ printf&#40; &#34;Enter a sentence of four words with &#8230; <a href="http://blog.ez2learn.com/2008/09/29/microsoft-never-follows-the-standard/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>問題</h2>
<p>今天有人問了這樣一個問題，如何不讓scanf取得指定長度後剩下的字元，被下一個scanf取得</p>
<h2>解法</h2>
<p>首先我想到的是scanf一個字串，可是如果字串長度剛好，就會造成要再輸入一次，於是我又想到了fflush來清空stdin的緩衝區，查了MSDN，也寫了一個小程式來測試，果然可行，但是&#8230;</p>
<p><span id="more-33"></span></p>
<h2>但是</h2>
<p>但是有人說fflush(stdin)不合C語言標準，起先我懷疑真的是這樣嗎? 因為我查MSDN是這樣寫沒錯阿，還有範例示範呢</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* FFLUSH.C */</span>
&nbsp;
<span style="color: #339933;">#include</span>
<span style="color: #339933;">#include </span>
&nbsp;
<span style="color: #993333;">void</span> main<span style="color: #009900;">&#40;</span> <span style="color: #993333;">void</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
   <span style="color: #993333;">int</span> integer<span style="color: #339933;">;</span>
   <span style="color: #993333;">char</span> string<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">81</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;">/* Read each word as a string. */</span>
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Enter a sentence of four words with scanf: &quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span> integer <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> integer <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> integer<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      scanf<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> string <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> string <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #808080; font-style: italic;">/* You must flush the input buffer before using gets. */</span>
   fflush<span style="color: #009900;">&#40;</span> stdin <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Enter the same sentence with gets: &quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   gets<span style="color: #009900;">&#40;</span> string <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> string <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>上網一找，找到文章說fflush果然不是標準</p>
<p><a href="http://cpp.ga-la.com/html/3/3/0510/6.htm">文章</a></p>
<p>正確的做法應該要自己一個一個地吃掉字元，話說M$真的很不愛守標準，我一直都很相信MSDN，沒想到今天被它給騙了 囧rz，下次看MSDN應該要帶一點懷疑，不過不可否認地，在這裡這樣的不標準確實比標準來得方便</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ez2learn.com/2008/09/29/microsoft-never-follows-the-standard/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

