<?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>Hacklog &#187; 编程</title>
	<atom:link href="http://ihacklog.com/cat/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://ihacklog.com</link>
	<description>荒野无灯weblog</description>
	<lastBuildDate>Sat, 04 Feb 2012 12:30:34 +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>数学常数E的含义[转]</title>
		<link>http://ihacklog.com/programming/math/an-intuitive-guide-to-exponential-functions-e.html</link>
		<comments>http://ihacklog.com/programming/math/an-intuitive-guide-to-exponential-functions-e.html#comments</comments>
		<pubDate>Sun, 18 Dec 2011 01:17:19 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=5137</guid>
		<description><![CDATA[很少转载文章的，这次看到这个东西，觉得非常有必要转一下。（PS:我写的Hacklog Remote Image Autosave，这次终于自己用上了~~ ） 1. e是一个重要的常数，但是我一直不知道，它的真正含义是什么。 它不像π。大家都知道，π代表了圆的周长与直径之比3.14159，可是如果我问你，e代表了什么。你能回答吗？ 维基百科说： “e是自然对数的底数。” 但是，你去看“自然对数”，得到的解释却是： “自然对数是以e为底的对数函数，e是一个无理数，约等于2.718281828。” 这就构成了循...]]></description>
			<content:encoded><![CDATA[<p>很少转载文章的，这次看到这个东西，觉得非常有必要转一下。（PS:我写的Hacklog Remote Image Autosave，这次终于自己用上了~~ <img src='http://ihacklog.com/wordpress/wp-includes/images/smilies/icon_arrow.gif' alt=':arrow:' class='wp-smiley' />  ）</p>
<p>1.</p>
<div id="main-content">
<div>
<p>e是一个重要的常数，但是我一直不知道，它的真正含义是什么。</p>
</div>
<div id="more">
<p>它不像π。大家都知道，π代表了圆的周长与直径之比3.14159，可是如果我问你，e代表了什么。你能回答吗？</p>
<p><a href="http://en.wikipedia.org/wiki/E_(mathematical_constant)" target="_blank">维基百科</a>说：</p>
<blockquote><p>“e是自然对数的底数。”</p></blockquote>
<p>但是，你去看<a href="http://en.wikipedia.org/wiki/Natural_logarithm" target="_blank">“自然对数”</a>，得到的解释却是：</p>
<blockquote><p>“自然对数是以e为底的对数函数，e是一个无理数，约等于2.718281828。”</p></blockquote>
<p>这就构成了循环定义，完全没有说e是什么。数学家选择这样一个无理数作为底数，还号称这种对数很”自然”，这难道不是很奇怪的事情吗？</p>
<p><img src="http://static.ihacklog.com/wp-files/2011/12/bg2011070901.png" alt="" /></p>
<p>2.</p>
<p>昨天我读到一篇<a href="http://betterexplained.com/articles/an-intuitive-guide-to-exponential-functions-e/" target="_blank">好文章</a>，它把这个问题解释得非常清楚，而且一看就懂。</p>
<p>它说，什么是e？简单说，<strong>e就是增长的极限。</strong></p>
<p>下面就是它的解释。</p>
<p>3.</p>
<p>假定有一种单细胞生物，它每过24小时分裂一次。</p>
<p>那么很显然，这种生物的数量，每天都会翻一倍。今天是1个，明天就是2个，后天就是4个。我们可以写出一个增长数量的公式：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D2%5Ex&amp;chs=30" alt="" /></p>
<p>上式中的x就表示天数。这种生物在x天的总数，就是2的x次方。这个式子可以被改成下面这样：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%20%3D%20(1%20%2B%20100%25)%5Ex&amp;chs=30" alt="" /></p>
<p>其中，1表示原有数量，100%表示单位时间内的增长率。</p>
<p>4.</p>
<p>我们继续假定：每过12个小时，也就是分裂进行到一半的时候，新产生的那半个细胞已经可以再次分裂了。</p>
<p>因此，一天24个小时可以分成两个阶段，每一个阶段都在前一个阶段的基础上增长50%。</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D(1%2B%5Cfrac%7B100%25%7D%7B2%7D)%5E2%3D2.25&amp;chs=50" alt="" /></p>
<p>当这一天结束的时候，我们一共得到了2.25个细胞。其中，1个是原有的，1个是新生的，另外的0.25个是新生细胞分裂到一半的。</p>
<p>如果我们继续修改假设，这种细胞每过8小时就具备独立分裂的能力，也就是将1天分成3个阶段。</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D(1%2B%5Cfrac%7B100%25%7D%7B3%7D)%5E3%3D2.37037...&amp;chs=50" alt="" /></p>
<p>那么，最后我们就可以得到大约2.37个细胞。</p>
<p>很自然地，如果我们进一步设想，这种分裂是连续不断进行的，新生细胞每分每秒都具备继续分裂的能力，那么一天最多可以得到多少个细胞呢？</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D(1%2B%5Cfrac%7B100%25%7D%7Bn%7D)%5En%3D%3F&amp;chs=50" alt="" /></p>
<p>当n趋向无限时，这个式子的极值等于2.718281828&#8230;。</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=%5Clim_%7Bn%5Cto%5Cinfty%7D(1%2B%5Cfrac%7B100%25%7D%7Bn%7D)%5En%3D2.718281828...&amp;chs=50" alt="" /></p>
<p>因此，当增长率为100%保持不变时，我们在单位时间内最多只能得到2.71828个细胞。<strong>数学家把这个数就称为e，它的含义是单位时间内，持续的翻倍增长所能达到的极限值。</strong></p>
<p>这个值是自然增长的极限，因此以e为底的对数，就叫做自然对数。</p>
<p>5.</p>
<p>有了这个值以后，计算银行的复利就非常容易。</p>
<p>假定有一家银行，每年的复利是100%，请问存入100元，一年后可以拿多少钱？</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=%5Clim_%7Bn%5Cto%5Cinfty%7D100(1%2B%5Cfrac%7B100%25%7D%7Bn%7D)%5En%3D100e%3D271.828...&amp;chs=50" alt="" /></p>
<p>回答就是271.828元，等于100个e。</p>
<p>但是，实际生活中，银行的利息没有这么高，如果利息率只有5%，那么100元存一年可以拿到多少钱呢？</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=%5Clim_%7Bn%5Cto%5Cinfty%7D100(1%2B%5Cfrac%7B5%25%7D%7Bn%7D)%5En%3D%3F&amp;chs=50" alt="" /></p>
<p>为了便于思考，我们取n等于50：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=100(1%2B%5Cfrac%7B5%25%7D%7B50%7D)%5E%7B50%7D%3D100(1%2B0.1%25)%5E%7B50%7D&amp;chs=50" alt="" /></p>
<p>我们知道，在100%利息率的情况下，n=1000所得到的值非常接近e：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=(1%2B%5Cfrac%7B100%25%7D%7B1000%7D)%5E%7B1000%7D%3D(1%2B0.1%25)%5E%7B1000%7D%5Capprox%20e&amp;chs=50" alt="" /></p>
<p>因此，5%利息率就相当于e的20分之一次方：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=(1%2B%5Cfrac%7B5%25%7D%7B50%7D)%5E%7B50%7D%3D%5B(1%2B%5Cfrac%7B100%25%7D%7B1000%7D)%5E%7B1000%7D%5D%5E%7B%5Cfrac%7B1%7D%7B20%7D%7D%5Capprox%20e%5E%7B%5Cfrac%7B1%7D%7B20%7D%7D&amp;chs=50" alt="" /></p>
<p>20分之一正好等于5%的利率率，所以我们可以把公式改写成：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3De%5E%7Brate%7D&amp;chs=25" alt="" /></p>
<p>上式的rate就代表增长率。这说明e可以用于任何增长率的计算，前提是它必须是持续不断的复合式增长。</p>
<p>6.</p>
<p>再考虑时间因素，如果把钱在银行里存2年，可以得到多少钱？</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D(e%5E%7Br%7D)%5E2%3De%5E%7B2r%7D&amp;chs=30" alt="" /></p>
<p>在时间t的情况下，通用公式就是：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=growth%3D(e%5E%7Br%7D)%5Et%3De%5E%7Br%5Ccdot%20t%7D%3De%5E%7Brt%7D&amp;chs=30" alt="" /></p>
<p>上式就是计算增长量的万能公式，可以适用于任何时间、任何增长率。</p>
<p>7.</p>
<p>回到上面的例子，如果银行的利息率是5%的复利，请问100元存款翻倍需要多少时间？</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=100%5Ccdot%20e%5E%7B5%25t%7D%20%3D%20200&amp;chs=25" alt="" /></p>
<p>计算结果是13.86年：</p>
<p><img src="http://chart.googleapis.com/chart?cht=tx&amp;chl=t%3D%5Cfrac%7Bln2%7D%7B5%25%7D%3D%5Cfrac%7B0.693%7D%7B5%25%7D%3D%5Cfrac%7B69.3%7D%7B5%7D%5Capprox%20%5Cfrac%7B72%7D%7B5%7D&amp;chs=50" alt="" /></p>
<p>上式最后一个等号，表明用72除以增长率，可以得到翻倍的大致时间，这就是<a href="http://zh.wikipedia.org/wiki/72%E6%B3%95%E5%89%87" target="_blank">72法则</a>的来源。</p>
<p>（完）</p>
</div>
</div>
<div>
<span>文档信息</span></p>
<ul>
<li>版权声明：自由转载-非商用-非衍生-保持署名 | <a href="http://creativecommons.org/licenses/by-nc-nd/3.0/deed.zh">Creative Commons BY-NC-ND 3.0</a></li>
<li>原文网址：<a href="http://www.ruanyifeng.com/blog/2011/07/mathematical_constant_e.html">http://www.ruanyifeng.com/blog/2011/07/mathematical_constant_e.html</a></li>
<li>最后修改时间：2011年11月17日 13:07</li>
</ul>
</div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=5137";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=5137" title="Permanent Link to 数学常数E的含义[转]" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=5137</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2010年02月28日  //  <a href="http://ihacklog.com/software/cool_software_share/pro-tool-chen-feng-usb-drive-systemv2-0.html" title="电脑城装机专用工具：晨枫U盘维护工具V2.0">电脑城装机专用工具：晨枫U盘维护工具V2.0</a> (3)</li><li>2011年08月1日  //  <a href="http://ihacklog.com/php/skills/smart-file-type-detection-using-php.html" title="利用PHP实现智能文件类型检测">利用PHP实现智能文件类型检测</a> (3)</li><li>2011年07月11日  //  <a href="http://ihacklog.com/software/cool_software_share/five-best-fonts-for-programmers.html" title="分享5个程序员专用字体">分享5个程序员专用字体</a> (7)</li><li>2009年05月24日  //  <a href="http://ihacklog.com/memory/wangyouzaochudaibiaocaonimaxinzitu.html" title="网友造出代表“草泥马”新字(图)">网友造出代表“草泥马”新字(图)</a> (3)</li><li>2011年08月22日  //  <a href="http://ihacklog.com/software/tips/fuck-windows-7-right-click-context-menu.html" title="玩转windows7右键菜单">玩转windows7右键菜单</a> (0)</li><li>2009年08月14日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/ubuntuqiyong3dxiaoguohouchuxiancuowu.html" title="UBUNTU启用3d效果后出现错误">UBUNTU启用3d效果后出现错误</a> (1)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/math/an-intuitive-guide-to-exponential-functions-e.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>BlowFish 加密算法Bcrypt</title>
		<link>http://ihacklog.com/programming/c/blowfish-cipher.html</link>
		<comments>http://ihacklog.com/programming/c/blowfish-cipher.html#comments</comments>
		<pubDate>Fri, 14 Oct 2011 14:56:57 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4939</guid>
		<description><![CDATA[Blowfish是1993年布鲁斯·施奈尔(Bruce Schneier)开发的对称密钥区块加密算法，区块长为64位，密钥为1至448位的可变长度。与DES等算法相比，其处理速度较快。因为其无须授权即可使用，作为一种自由授权的加密方式在SSH、文件加密软件等被广泛地使用。 关于此算法的发明者： 布鲁斯·施奈尔 （Bruce Schneier，1963年1月15日－）是一位美国的密码学学者、信息安全专家与作家。他撰写了数本信息安全与密码学相关的书籍，并且创办了BT公司并担任其首席技术官(CTO)。 分组密码...]]></description>
			<content:encoded><![CDATA[<blockquote><p>Blowfish是1993年布鲁斯·施奈尔(Bruce Schneier)开发的对称密钥区块加密算法，区块长为64位，密钥为1至448位的可变长度。与DES等算法相比，其处理速度较快。因为其无须授权即可使用，作为一种自由授权的加密方式在SSH、文件加密软件等被广泛地使用。</p></blockquote>
<p>关于此算法的发明者：</p>
<blockquote><p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2FBruce_Schneier" target="_blank">布鲁斯·施奈尔</a> （Bruce Schneier，1963年1月15日－）是一位美国的密码学学者、信息安全专家与作家。他撰写了数本信息安全与密码学相关的书籍，并且创办了BT公司并担任其首席技术官(CTO)。</p></blockquote>
<h3>分组密码(Block cypher)</h3>
<p>分组密码（Block cypher，又称<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E5%25A1%258A%25E5%25AF%2586%25E7%25A2%25BC" target="_blank">分块密码</a>），是一种对称密钥密码。它的特点是将明文分成多个等长的组，并用相同的密码算法和密钥对每组分别进行加密和解密。其中典型的如DES和AES作为美国政府核定的标准加密算法，应用领域从电子邮件加密到银行交易转帐，非常广泛。<br />
blowfish属于分组密码中的一种。</p>
<p>要了解blowfish加密算法，也许我们应该先了解一下<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FFeistel_cipher" target="_blank">Feistel 密码</a>,因为blowfish加密算法也是feistel密码算法中的一种：<br />
假设F是轮函数，然后K<sub>0</sub>到K<sub>n</sub>作为0到n轮的sub-keys,<br />
基本的操作如下：</p>
<div style="font-size:16px;">
把明文分为相等的两块：L<sub>0</sub>和R<sub>0</sub>,<br />
对于每一轮i=0,1,2,&#8230;,n 进行如下运算：<br />
L<sub>i+1</sub> = R<sub>i</sub><br />
R<sub>i+1</sub> = L<sub>i</sub> ^ F(R<sub>i</sub>,K<sub>i</sub>)<br />
最后得到密文(R<sub>n+1</sub>,L<sub>n+1</sub>)<br />
解密(R<sub>n+1</sub>,L<sub>n+1</sub>)是通过如下步骤来完成的：<br />
令i=n,n-1,&#8230;,0<br />
R<sub>i</sub> = L<sub>i+1</sub><br />
L<sub>i</sub> = R<sub>i+1</sub> ^ F(L<sub>i+1</sub>,K<sub>i</sub>)<br />
最后又得到原来的(L<sub>0</sub>,R<sub>0</sub>)了
</div>
<p>这么说可能不好理解，看下图（来自wikipedia)就清楚了：<br />
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/Feistel_cipher_diagram_en.svg/511px-Feistel_cipher_diagram_en.svg.png" alt="feistel cypher" style="background-color:#cdcdcd;"/><br />
简单地说：<br />
就是一种分组密码，一般分为64位一组，一组分左右部分，进行一般为16轮的迭代运算，每次迭代完后交换左右位置，可以自己进行设计的有:<br />
分组大小<br />
密钥长度<br />
轮次数<br />
子密钥生成<br />
轮函数</p>
<p>然后应该了解一下<strong><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%E5%9D%97%E5%AF%86%E7%A0%81%E7%9A%84%E5%B7%A5%E4%BD%9C%E6%A8%A1%E5%BC%8F" target="_blank">块密码的工作模式</a></strong>：<br />
from wikipedia:</p>
<blockquote><p>密码学中，块密码的工作模式允许使用同一个块密码密钥对多于一块的数据进行加密，并保证其安全性。[1][2] 块密码自身只能加密长度等于密码块长度的单块数据，若要加密变长数据，则数据必须先被划分为一些单独的密码块。通常而言，最后一块数据也需要使用合适填充方式将数据扩展到符合密码块大小的长度。一种工作模式描述了加密每一数据块的过程，并常常使用基于一个通常称为初始化向量的附加输入值以进行随机化，以保证安全[1]。</p>
<p>工作模式主要用来进行加密和认证。[1][3] 对加密模式的研究曾经包含数据的完整性保护，即在某些数据被修改后的情况下密码的误差传播特性。后来的研究则将完整性保护作为另一个完全不同的，与加密无关的密码学目标。部分现代的工作模式用有效的方法将加密和认证结合起来，称为认证加密模式。[2]</p>
<p>虽然工作模式通常应用于对称加密[2]，它亦可以应用于公钥加密，例如在原理上对RSA进行处理，但在实用中，公钥密码学通常不用于加密较长的信息，而是使用混合加密方案</p></blockquote>
<p>一般来说常用的加密模式有这么几种：</p>
<blockquote><p> 电子密码本（ECB）<br />
 密码块链接（CBC）：在CBC模式中，每个平文块先与前一个密文块进行异或后，再进行加密。在这种方法中，每个密文块都依赖于它前面的所有平文块。同时，为了保证每条消息的唯一性，在第一个块中需要使用初始化向量。<br />
 填充密码块链接（PCBC）<br />
 密文反馈（CFB）<br />
 输出反馈（OFB）<br />
 计数器模式（CTR）</p></blockquote>
<h3>初始化向量（IV）</h3>
<blockquote><p>初始化向量（IV，Initialization Vector）是许多工作模式中用于随机化加密的一块数据，因此可以由相同的明文，相同的密钥产生不同的密文，而无需重新产生密钥，避免了通常相当复杂的这一过程。</p>
<p>初始化向量与密钥相比有不同的安全性需求，因此IV通常无须保密，然而在大多数情况中，不应当在使用同一密钥的情况下两次使用同一个IV。对于CBC和CFB，重用IV会导致泄露平文首个块的某些信息，亦包括两个不同消息中相同的前缀。对于OFB和CTR而言，重用IV会导致完全失去安全性。另外，在CBC模式中，IV在加密时必须是无法预测的；特别的，在许多实现中使用的产生IV的方法，例如SSL2.0使用的，即采用上一个消息的最后一块密文作为下一个消息的IV，是不安全的[12]。</p></blockquote>
<h3>填充</h3>
<blockquote><p>块密码只能对确定长度的数据块进行处理，而消息的长度通常是可变的。因此部分模式（即ECB和CBC）需要最后一块在加密前进行填充。有数种填充方法，其中最简单的一种是在平文的最后填充空字符以使其长度为块长度的整数倍，但必须保证可以恢复平文的原始长度；例如，若平文是C语言风格的字符串，则只有串尾会有空字符。稍微复杂一点的方法则是原始的DES使用的方法，即在数据后添加一个1位，再添加足够的0位直到满足块长度的要求；若消息长度刚好符合块长度，则添加一个填充块。最复杂的则是针对CBC的方法，例如密文窃取，残块终结等，不会产生额外的密文，但会增加一些复杂度。布鲁斯·施奈尔和尼尔斯·弗格森提出了两种简单的可能性：添加一个值为128的字节（十六进制的80），再以0字节填满最后一个块；或向最后一个块填充n个值均为n的字节[13]。</p>
<p>CFB，OFB和CTR模式不需要对长度不为密码块大小整数倍的消息进行特别的处理。因为这些模式是通过对块密码的输出与平文进行异或工作的。最后一个平文块（可能是不完整的）与密钥流块的前几个字节异或后，产生了与该平文块大小相同的密文块。流密码的这个特性使得它们可以应用在需要密文和平文数据长度严格相等的场合，也可以应用在以流形式传输数据而不便于进行填充的场合。</p></blockquote>
<p>另外在mcrypt的man文档里也找到了加密模式的介绍：</p>
<h3>加密模式</h3>
<blockquote><p>modes of encryption:<br />
<strong>ECB</strong>:  The Electronic CodeBook mode. It is the simplest mode to use with a block cipher. Encrypts each block independently. </p>
<p><strong>CBC</strong>: The Cipher Block Chaining mode. It is better than ECB since the plaintext is XOR&#8217;ed with the previous ciphertext. A random block is placed as the first block so the same block or messages always encrypt to something different. (This is the default mode) </p>
<p><strong>CFB</strong>: The Cipher-Feedback Mode (in 8bit). This is a self-synchronizing stream cipher implemented from a block cipher. </p>
<p><strong>OFB</strong>: The Output-Feedback Mode (in 8bit). This is a synchronous stream cipher implemented from a block cipher. It is intended for use in noisy lines, because corrupted ciphertext blocks do not corrupt the plaintext blocks that follow. Insecure (because used in 8bit mode) so I recommend against using it. Added just for completeness. </p>
<p><strong>nOFB</strong>: The Output-Feedback Mode (in nbit). n Is the size of the block of the algorithm. This is a synchronous stream cipher implemented from a block cipher. It is intended for use in noisy lines, because corrupted ciphertext blocks do not corrupt the plaintext blocks that follow.</p></blockquote>
<p>via http://mcrypt.hellug.gr/mcrypt/mcrypt.1.html<br />
BTW,The author of mcrypt and libmcrypt is <strong>Nikos Mavroyanopoulos</strong>.</p>
<h3>分块大小</h3>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBlock_size_(cryptography)" target="_blank">Block size</a></p>
<blockquote><p>现代加密算法中，对称密钥算法通常被分为stream ciphers和block ciphers。分块密码处理固定长度的字符串位数(bit)。这个比特字符串的长度必须和分块大小一样长。输入（明文）和输出（密文）都是同样长度的。输出不能比输入短——这是遵循<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPigeonhole_principle" target="_blank">Pigeonhole principle</a>（鸽巢原理，又名狄利克雷抽屉原理、鸽笼原理）和密码必须是可逆的事实——然而，输出比输入更长又是不可取的。</p></blockquote>
<p>鸽巢原理：</p>
<blockquote><p>其中一种简单的表述法为：<br />
    若有n个笼子和n+1只鸽子，所有的鸽子都被关在鸽笼里，那么至少有一个笼子有至少2只鸽子。<br />
另一种为：<br />
    若有n个笼子和kn+1只鸽子，所有的鸽子都被关在鸽笼里，那么至少有一个笼子有至少k+1只鸽子。<br />
拉姆齐定理是此原理的推广。</p></blockquote>
<p>通俗点说：<br />
10只鸽子放进9个鸽笼，那么一定有一个鸽笼放进了至少两只鸽子。</p>
<p>S-box (Substitution-box,替换盒)<br />
在分块密码中，S-box通常被用来掩盖密钥和密文之间的关系。</p>
<blockquote><p>In general, an S-Box takes some number of input bits, m, and transforms them into some number of output bits, n: an m×n S-Box can be implemented as a lookup table with 2m words of n bits each. Fixed tables are normally used, as in the Data Encryption Standard (DES), but in some ciphers the tables are generated dynamically from the key; e.g. the Blowfish and the Twofish encryption algorithms. Bruce Schneier describes IDEA&#8217;s modular multiplication step as a key-dependent S-Box.</p></blockquote>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FInternational_Data_Encryption_Algorithm" target="_blank">IDEA(International Data Encryption Algorithm)</a></p>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRijndael_S-box" target="_blank">Rijndael S-box</a></p>
<p>算法说明：<br />
BlowFish 使用了两个box,除了著名的S-box，还有一个p-box<br />
pbox有18个unsigned long元素<br />
sbox有4&#215;256个unsigned long元素<br />
  BlowFish算法中，有一个核心加密函数，该函数输入64位信息，运算后， 以64位密文的形式输出。 用BlowFish算法加密信息，需要两个过程：<br />
这里我看的源码是<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPaul_Kocher" target="_blank">Paul Kocher</a>的C语言版本。<br />
源码下载地址：<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.schneier.com%2Fblowfish-download.html" target="_blank">http://www.schneier.com/blowfish-download.html</a></p>
<p>1.密钥预处理<br />
2.信息加密</p>
<p>结构体定义：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">16</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000dd;">256</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span> BLOWFISH_CTX<span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>1.密钥预处理<br />
  BlowFish算法的源密钥——pbox和sbox是固定的。此pbox和sbox的值来自<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.super-computing.org%2Fpi-hexa_current.html" target="_blank">PI的十六进制数字值</a>，使用这些数的原因是这些数看不出有什么明显的规律。(<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FPi" target="_blank">PI</a>)<br />
这里摘取1-500位：</p>
<pre class='smooth'>
3.243F6A8885 A308D31319 8A2E037073 44A4093822 299F31D008
  2EFA98EC4E 6C89452821 E638D01377 BE5466CF34 E90C6CC0AC
  29B7C97C50 DD3F84D5B5 B547091792 16D5D98979 FB1BD1310B
  A698DFB5AC 2FFD72DBD0 1ADFB7B8E1 AFED6A267E 96BA7C9045
  F12C7F9924 A19947B391 6CF70801F2 E2858EFC16 636920D871
  574E69A458 FEA3F4933D 7E0D95748F 728EB65871 8BCD588215
  4AEE7B54A4 1DC25A59B5 9C30D5392A F26013C5D1 B023286085
  F0CA417918 B8DB38EF8E 79DCB0603A 180E6C9E0E 8BB01E8A3E
  D71577C1BD 314B2778AF 2FDA55605C 60E65525F3 AA55AB9457
  48986263E8 144055CA39 6A2AAB10B6 B4CC5C3411 41E8CEA154
</pre>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Init<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>key<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> keyLen<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">,</span> j<span style="color: #339933;">,</span> k<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> data<span style="color: #339933;">,</span> datal<span style="color: #339933;">,</span> datar<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">256</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ORIG_S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; data <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>k <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; data <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>data <span style="color: #339933;">&lt;&lt;</span> <span style="color: #0000dd;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">|</span> key<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; j <span style="color: #339933;">=</span> j <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">&gt;=</span> keyLen<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> ORIG_P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">^</span> data<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; datal <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
&nbsp; datar <span style="color: #339933;">=</span> <span style="color: #208080;">0x00000000</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Blowfish_Encrypt<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datal<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datar<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>j <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">256</span><span style="color: #339933;">;</span> j <span style="color: #339933;">+=</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; Blowfish_Encrypt<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datal<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>datar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datal<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>j <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> datar<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>我们要加密一个信息，<br />
  需要自己选择一个key，用这个key对pbox和sbox进行变换，得到下一步信息加密<br />
  所要用的pbox和sbox。<br />
  具体的变化算法如下：<br />
  用原sbox: ORIG_S 填充 sbox</p>
<p>  然后，每次取key与data进行运算，运算后的结果送给pbox<br />
  运算过程是这样的：<br />
  进行N+2次运算（N=16),<br />
  令 32位无符号data为0,由于Key是unsigned char类型的，每次对data左移8位(一个字节)之后与<br />
  相应的key相或（即相加），当key长度小于4时，循环使用Key。</p>
<p>接下来，用bf_encypt加密一个全0的64位信息（分为datal和datar,各32位）<br />
用输出的结果datal和datar分别替换pbox[0]和pbox[1]<br />
然后，继续加密datal和datar,用输出结果替换pbox[2]和pbox[3]<br />
&#8230;&#8230;<br />
这样循环18次，把pbox全部替换完成。</p>
<p>接下来是对sbox的替换了。<br />
这次总共是循环4&#215;256次，每次循环的过程与上面的一样。<br />
不过这里用的datal和datar就是上面运算过后的datal和datar.</p>
<p>2.信息加密<br />
接下来看这个加密函数，上面的初始化过程已经用到它了。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Encrypt<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xr<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">short</span> &nbsp; &nbsp; &nbsp; i<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xl <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> N<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> F<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> Xl<span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> Xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xr <span style="color: #339933;">=</span> Xr <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>N<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>N <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #339933;">*</span>xl <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #339933;">*</span>xr <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>与上面说的Feistel 密码的运算过程是一样的：<br />
把64位的明文分为l和r<br />
进行16轮运算：<br />
令i=0,1,2,3,&#8230;,N-1<br />
 Xl = Xl ^ ctx->P[i];<br />
 Xr = F(ctx, Xl) ^ Xr;<br />
若i&lt;N,交换Xl和Xr的值继续做上述运算。<br />
16轮运算完了之后，再做一次Xl与Xr的交换，因为第16次运算完了之后，实际上<br />
没有再做运算了，但是Xl与Xr还是交换了，因此，得换回来。</p>
<p>然后，Xr = Xr ^ ctx->P[N];<br />
  Xl = Xl ^ ctx->P[N + 1];<br />
现在的Xl和Xr就是加密后的数据了。<br />
加密过程图解：<br />
<img src="http://upload.wikimedia.org/wikipedia/en/3/34/BlowfishDiagram.png" alt="blowfish encryption" /></p>
<p>这加密过程中用到了F变换：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">static</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> F<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span> a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #339933;">,</span> d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp;<span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;y<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;d <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;c <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;b <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;x <span style="color: #339933;">&gt;&gt;=</span> <span style="color: #0000dd;">8</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;a <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">unsigned</span> <span style="color: #993333;">short</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&amp;</span> <span style="color: #208080;">0xFF</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>b<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> y <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp;y <span style="color: #339933;">=</span> y <span style="color: #339933;">+</span> ctx<span style="color: #339933;">-&gt;</span>S<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #b1b100;">return</span> y<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这个变换是这样的：<br />
它接收一个32位的数据，然后从高位到低位分为四段，每段8位，依次给<br />
a,b,c,d<br />
取sbox[0][a]+sbox[1][b]，相加后的结果再与sbox[2][c]做异或运算，<br />
得出的结果再加上sbox[3][d]<br />
由于sbox的元素都是32位的，因此，F变换后的输出也是32位的。<br />
轮函数图解：<br />
<img src="http://upload.wikimedia.org/wikipedia/en/thumb/2/22/BlowfishFFunction.svg/250px-BlowfishFFunction.svg.png" alt="blowfish F function" style="background-color:#ddd;"/></p>
<p>解密函数：<br />
可以看到解密函数与加密函数非常的相似，只是把p0,p1,&#8230;,p17 逆序使用：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">void</span> Blowfish_Decrypt<span style="color: #009900;">&#40;</span>BLOWFISH_CTX <span style="color: #339933;">*</span>ctx<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">,</span> <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> <span style="color: #339933;">*</span>xr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;Xr<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span> &nbsp;temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #993333;">short</span> &nbsp; &nbsp; &nbsp; i<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xl <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xl<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> <span style="color: #339933;">*</span>xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> N <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">--</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> F<span style="color: #009900;">&#40;</span>ctx<span style="color: #339933;">,</span> Xl<span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> Xr<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/* Exchange Xl and Xr */</span><br />
&nbsp; &nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #808080; font-style: italic;">/* Exchange Xl and Xr */</span><br />
&nbsp; temp <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
&nbsp; Xr <span style="color: #339933;">=</span> temp<span style="color: #339933;">;</span><br />
<br />
&nbsp; Xr <span style="color: #339933;">=</span> Xr <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; Xl <span style="color: #339933;">=</span> Xl <span style="color: #339933;">^</span> ctx<span style="color: #339933;">-&gt;</span>P<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #339933;">*</span>xl <span style="color: #339933;">=</span> Xl<span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #339933;">*</span>xr <span style="color: #339933;">=</span> Xr<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>关于BlowFish 密码，个人认为可以把它理解为一种复杂的<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%E5%BC%82%E6%88%96%E5%AF%86%E7%A0%81" target="_blank">异或密码</a>,异或运算的规律：</p>
<pre class='smooth'>
A ^ 0 = A
A ^ A = 0
(A ^ B) ^ C = A ^ (B ^ C)
(B ^ A) ^ A = B ^ 0 = B
</pre>
<p>按这种逻辑，文本序列的每个字符可以通过与给定的密钥进行按位异或运算来加密。如果要解密，只需要将加密後的结果与密钥再次进行按位异或运算即可。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4939";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4939" title="Permanent Link to BlowFish 加密算法Bcrypt" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4939</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2011年03月18日  //  <a href="http://ihacklog.com/programming/c/filecombine.html" title="文本文件批量合并小工具">文本文件批量合并小工具</a> (15)</li><li>2010年03月18日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/mp3-id3-info-convert-into-utf-8-format.html" title="转换id3信息为utf-8解决linux下面mp3乱码问题">转换id3信息为utf-8解决linux下面mp3乱码问题</a> (11)</li><li>2011年04月13日  //  <a href="http://ihacklog.com/server/ruijie-startup-as-servcie.html" title="折腾锐捷开机自动启动认证">折腾锐捷开机自动启动认证</a> (7)</li><li>2009年01月7日  //  <a href="http://ihacklog.com/php/windows%e4%b8%8bsvn%e5%ae%a2%e6%88%b7%e7%ab%aftortoisesvn%e7%9a%84%e5%ae%89%e8%a3%85%e5%92%8c%e6%93%8d%e4%bd%9c.html" title="Windows下svn客户端TortoiseSVN的安装和操作">Windows下svn客户端TortoiseSVN的安装和操作</a> (0)</li><li>2008年12月6日  //  <a href="http://ihacklog.com/php/discuz/music-plugin-installation-importing-tables.html" title="关于音乐插件的安装之数据库文件导入">关于音乐插件的安装之数据库文件导入</a> (1)</li><li>2009年04月26日  //  <a href="http://ihacklog.com/xml_html_css_js_ajax/14geguanyutuxiangchulidejqueryplugins.html" title="14个关于图像处理的jQuery Plugins">14个关于图像处理的jQuery Plugins</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/blowfish-cipher.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MIT &amp; Stanford CS 公开课视频</title>
		<link>http://ihacklog.com/programming/mit-stanford-cs-open-cource.html</link>
		<comments>http://ihacklog.com/programming/mit-stanford-cs-open-cource.html#comments</comments>
		<pubDate>Sat, 08 Oct 2011 19:08:10 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[编程]]></category>
		<category><![CDATA[MIT]]></category>
		<category><![CDATA[Stanford]]></category>
		<category><![CDATA[公开课]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4928</guid>
		<description><![CDATA[入门级别： 斯坦福大学公开课：编程方法学 语言：JAVA DEMO: 第5集：变量 麻省理工学院公开课：计算机科学及编程导论 语言：Python DEMO: [第1集] 课程目标，数据类型，运算，变量 进阶： 麻省理工学院公开课：算法导论 DEMO: [第1集]课程简介及算法分析 下载：http://www.verycd.com/topics/87348/ 喜欢这篇文章吗?请订阅本站 RSS feed 或 作者：荒野无灯 出处：Hacklog【荒野无灯weblog】 声明: 本站遵循 署名-非商业性使用-...]]></description>
			<content:encoded><![CDATA[<p>入门级别：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fv.163.com%2Fspecial%2Fsp%2Fprogramming.html" target="_blank">斯坦福大学公开课：编程方法学</a> 语言：JAVA<br />
DEMO: 第5集：变量<br />
<script type="text/javascript" reload="1">document.write(AC_FL_RunContent('width', '500', 'height', '400', 'allowNetworking', 'internal', 'allowScriptAccess', 'never', 'src', 'http://ihacklog.com/wordpress/wp-content/themes/SimpleDark/swf/flvplayer.swf', 'flashvars', 'file=http%3A%2F%2F111.8.9.211%2Fcemov.bn.netease.com%3A80%2Fmovie%2F2011%2F1%2FL%2F2%2FS6PLJBDL2.flv', 'quality', 'high', 'wmode', 'transparent', 'allowfullscreen', 'true'));</script></p>
<p><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fv.163.com%2Fspecial%2Fopencourse%2Fbianchengdaolun.html" target="_blank">麻省理工学院公开课：计算机科学及编程导论</a> 语言：Python<br />
DEMO: [第1集] 课程目标，数据类型，运算，变量<br />
<script type="text/javascript" reload="1">document.write(AC_FL_RunContent('width', '500', 'height', '400', 'allowNetworking', 'internal', 'allowScriptAccess', 'never', 'src', 'http://ihacklog.com/wordpress/wp-content/themes/SimpleDark/swf/flvplayer.swf', 'flashvars', 'file=http%3A%2F%2F111.8.8.101%2Fcemov.bn.netease.com%3A80%2Fmovie%2F2011%2F5%2F1%2FO%2FS745BK41O.flv', 'quality', 'high', 'wmode', 'transparent', 'allowfullscreen', 'true'));</script></p>
<p>进阶：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fv.163.com%2Fspecial%2Fopencourse%2Falgorithms.html" target="_blank">麻省理工学院公开课：算法导论</a><br />
DEMO: [第1集]课程简介及算法分析<br />
<script type="text/javascript" reload="1">document.write(AC_FL_RunContent('width', '500', 'height', '400', 'allowNetworking', 'internal', 'allowScriptAccess', 'never', 'src', 'http://ihacklog.com/wordpress/wp-content/themes/SimpleDark/swf/flvplayer.swf', 'flashvars', 'file=http%3A%2F%2F111.8.9.147%2Fmov.bn.netease.com%2Fmovie%2F2011%2F5%2F4%2FV%2FS72MEK54V.flv', 'quality', 'high', 'wmode', 'transparent', 'allowfullscreen', 'true'));</script><br />
下载：http://www.verycd.com/topics/87348/</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4928";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4928" title="Permanent Link to MIT &amp; Stanford CS 公开课视频" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4928</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2008年12月23日  //  <a href="http://ihacklog.com/php/vimvi-%e7%bc%96%e7%a8%8b%e6%8f%90%e9%80%9f.html" title="Vim/Vi 编程提速 ">Vim/Vi 编程提速 </a> (0)</li><li>2008年12月2日  //  <a href="http://ihacklog.com/software/tips/%e5%85%b3%e4%ba%8eadsl%e9%80%9f%e5%ba%a6%e6%ad%a3%e8%a7%a3.html" title="关于ADSL速度正解">关于ADSL速度正解</a> (0)</li><li>2012年01月7日  //  <a href="http://ihacklog.com/php/wordpress/plugins/three-wp-debuging-plugins.html" title="WP调试插件三款">WP调试插件三款</a> (0)</li><li>2011年12月20日  //  <a href="http://ihacklog.com/php/wordpress/plugins/hacklog-remote-attachment-upaiyun-version.html" title="Hacklog Remote Attachment Upyun(又拍云版) v1.2.6">Hacklog Remote Attachment Upyun(又拍云版) v1.2.6</a> (39)</li><li>2011年12月31日  //  <a href="http://ihacklog.com/memory/hacklog/last-day-of-2011-a-debug-day.html" title="2011年最后一天，de各种bug，唉">2011年最后一天，de各种bug，唉</a> (3)</li><li>2008年02月21日  //  <a href="http://ihacklog.com/php/skills/%e7%94%a8php%e5%ae%9a%e5%88%b6404%e9%94%99%e8%af%af%e9%a1%b5%e9%9d%a2%ef%bc%8c%e5%b9%b6%e5%8f%91%e4%bf%a1%e7%bb%99%e7%ae%a1%e7%90%86%e5%91%98%e7%9a%84%e7%a8%8b%e5%ba%8f.html" title="用php定制404错误页面，并发信给管理员的程序">用php定制404错误页面，并发信给管理员的程序</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/mit-stanford-cs-open-cource.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://111.8.8.101/cemov.bn.netease.com:80/movie/2011/5/1/O/S745BK41O.flv" length="1224" type="video/x-flv" />
<enclosure url="http://111.8.9.147/mov.bn.netease.com/movie/2011/5/4/V/S72MEK54V.flv" length="191469705" type="video/x-flv" />
<enclosure url="http://111.8.9.211/cemov.bn.netease.com:80/movie/2011/1/L/2/S6PLJBDL2.flv" length="1224" type="video/x-flv" />
		</item>
		<item>
		<title>Java学习之利用yahoo weather api 获取天气预报</title>
		<link>http://ihacklog.com/programming/java/learning-java-get-weather-report-via-yahoo-weather-api.html</link>
		<comments>http://ihacklog.com/programming/java/learning-java-get-weather-report-via-yahoo-weather-api.html#comments</comments>
		<pubDate>Sun, 24 Jul 2011 07:57:44 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[JAVA]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4684</guid>
		<description><![CDATA[在cnblogs看到 狼の禅 写的Java获取yahoo天气预报一文，于是有了这篇文章。 狼の禅 是将 对用HashMap 来保存在类中的，发现手动查找地点代码然后要一个个加进去还真麻烦，于是想下有没有偷懒的办法。果然，在雅虎 geoplanet 找到了答案。 最简单的办法是申请一个yahoo dev 的key ,然后就可以通过 GeoPlanet api 来查询相应地点的WOEID了。 狼の禅 用的是旧的p 参数传递地点代码，不过最新的api文档里面只对w参数作了说明，因此这里我就用WOEID了。 不想...]]></description>
			<content:encoded><![CDATA[<p>在cnblogs看到 狼の禅  写的<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.cnblogs.com%2Fkingwolfofsky%2Farchive%2F2011%2F07%2F23%2F2115098.html" target="_blank">Java获取yahoo天气预报</a>一文，于是有了这篇文章。<br />
 狼の禅 是将<地点代码,地名> 对用HashMap 来保存在类中的，发现手动查找地点代码然后要一个个加进去还真麻烦，于是想下有没有偷懒的办法。果然，在雅虎<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdeveloper.yahoo.com%2Fgeo%2Fgeoplanet%2Fdata%2F" target="_blank"> geoplanet </a>找到了答案。<br />
最简单的办法是申请一个yahoo dev 的key ,然后就可以通过 GeoPlanet api 来查询相应地点的<strong><a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.google.com.hk%2Fsearch%3Fclient%3Dopera%26rls%3Dzh-cn%26q%3DWOEID%26sourceid%3Dopera%26ie%3Dutf-8%26oe%3Dutf-8%26channel%3Dsuggest" target="_blank">WOEID</a></strong>了。<br />
狼の禅  用的是旧的p 参数传递地点代码，不过最新的api文档里面只对w参数作了说明，因此这里我就用<strong>WOEID</strong>了。<br />
不想注册申请key,还是自己折腾吧。<br />
下载最新的 <a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdeveloper.yahoo.com%2Fgeo%2Fgeoplanet%2Fdata%2FgetLatest.php" target="_blank">GeoPlanet Data</a> ,解压出来有这些个文件：</p>
<blockquote><p>The zip file below contains a license file, a readme file, and three data files in tab-delineated, Unicode (UTF-8) format:<br />
geoplanet_places_[version].tsv: the WOEID, the placename, and the WOEID of its parent entity<br />
geoplanet_aliases_[version].tsv: alternate names in multiple languages indexed against the WOEID<br />
geoplanet_adjacencies_[version].tsv: the entities neighboring each WOEID<br />
geoplanet_changes_[version].tsv: the list of removed WOEIDs and their replacement WOEID mappings</p></blockquote>
<p>这里我只取 geoplanet_places_7.6.0.tsv ，用EmEditor 打开，把 <strong>中国</strong> 地区的 COPY 出到另外一个文件<br />
<strong>Chinaplaces.tsv </strong>.<br />
这个tsv 文件是用tab分隔字段的，places文件的字段有：</p>
<blockquote><p>WOE_ID	ISO	Name	Language	PlaceType	Parent_ID</p></blockquote>
<p><a href="http://static.ihacklog.com/wp-files/2011/07/China_places.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/China_places-550x300.png" alt="" title="China_places" width="550" height="300" class="alignnone size-medium wp-image-4685" /> </a></p>
<p>直接解析这个tsv文件不怎么方便，还是用MySQL来搞定它吧。</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=CREATE"><span style="color: #990099; font-weight: bold;">CREATE</span></a> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/information-functions.html"><span style="color: #000099;">USER</span></a> <span style="color: #008000;">'geoplanet'</span>@<span style="color: #008000;">'localhost'</span> IDENTIFIED BY <span style="color: #008000;">'geoplanet'</span><span style="color: #000033;">;</span><br />
<br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=GRANT"><span style="color: #990099; font-weight: bold;">GRANT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=USAGE"><span style="color: #990099; font-weight: bold;">USAGE</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ON"><span style="color: #990099; font-weight: bold;">ON</span></a> <span style="color: #CC0099;">*</span> . <span style="color: #CC0099;">*</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TO"><span style="color: #990099; font-weight: bold;">TO</span></a> <span style="color: #008000;">'geoplanet'</span>@<span style="color: #008000;">'localhost'</span> IDENTIFIED BY <span style="color: #008000;">'geoplanet'</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=WITH"><span style="color: #990099; font-weight: bold;">WITH</span></a> MAX_QUERIES_PER_HOUR <span style="color: #008080;">0</span> MAX_CONNECTIONS_PER_HOUR <span style="color: #008080;">0</span> MAX_UPDATES_PER_HOUR <span style="color: #008080;">0</span> MAX_USER_CONNECTIONS <span style="color: #008080;">0</span> <span style="color: #000033;">;</span><br />
<br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=CREATE"><span style="color: #990099; font-weight: bold;">CREATE</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=DATABASE"><span style="color: #990099; font-weight: bold;">DATABASE</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=IF%20NOT%20EXISTS"><span style="color: #990099; font-weight: bold;"><a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/control-flow-functions.html"><span style="color: #009900;">IF</span></a> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> EXISTS</span></a> <span style="color: #008000;">`geoplanet`</span> <span style="color: #000033;">;</span><br />
<br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ALTER"><span style="color: #990099; font-weight: bold;">ALTER</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=DATABASE"><span style="color: #990099; font-weight: bold;">DATABASE</span></a> <span style="color: #008000;">`geoplanet`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=DEFAULT"><span style="color: #990099; font-weight: bold;">DEFAULT</span></a> CHARACTER <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=SET"><span style="color: #990099; font-weight: bold;">SET</span></a> utf8 <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">COLLATE</span></a> utf8_general_ci<span style="color: #000033;">;</span><br />
<br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=GRANT"><span style="color: #990099; font-weight: bold;">GRANT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ALL"><span style="color: #990099; font-weight: bold;">ALL</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=PRIVILEGES"><span style="color: #990099; font-weight: bold;">PRIVILEGES</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ON"><span style="color: #990099; font-weight: bold;">ON</span></a> <span style="color: #008000;">`geoplanet`</span> . <span style="color: #CC0099;">*</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TO"><span style="color: #990099; font-weight: bold;">TO</span></a> <span style="color: #008000;">'geoplanet'</span>@<span style="color: #008000;">'localhost'</span><span style="color: #000033;">;</span><br />
<br />
FLUSH <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=PRIVILEGES"><span style="color: #990099; font-weight: bold;">PRIVILEGES</span></a><span style="color: #000033;">;</span><br />
<br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=CREATE"><span style="color: #990099; font-weight: bold;">CREATE</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TABLE"><span style="color: #990099; font-weight: bold;">TABLE</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=IF%20NOT%20EXISTS"><span style="color: #990099; font-weight: bold;"><a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/control-flow-functions.html"><span style="color: #009900;">IF</span></a> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> EXISTS</span></a> <span style="color: #008000;">`places`</span> <span style="color: #FF00FF;">&#40;</span><br />
&nbsp; <span style="color: #008000;">`woe<span style="color: #008080; font-weight: bold;">_</span>id`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=VARCHAR"><span style="color: #999900; font-weight: bold;">varchar</span></a><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">15</span><span style="color: #FF00FF;">&#41;</span> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <span style="color: #008000;">`iso`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=VARCHAR"><span style="color: #999900; font-weight: bold;">varchar</span></a><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span><span style="color: #FF00FF;">&#41;</span> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <span style="color: #008000;">`name`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TEXT"><span style="color: #999900; font-weight: bold;">text</span></a> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <span style="color: #008000;">`language`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=VARCHAR"><span style="color: #999900; font-weight: bold;">varchar</span></a><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">6</span><span style="color: #FF00FF;">&#41;</span> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <span style="color: #008000;">`placetype`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=VARCHAR"><span style="color: #999900; font-weight: bold;">varchar</span></a><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">15</span><span style="color: #FF00FF;">&#41;</span> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <span style="color: #008000;">`parent<span style="color: #008080; font-weight: bold;">_</span><span style="color: #008080; font-weight: bold;">_</span>id`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=VARCHAR"><span style="color: #999900; font-weight: bold;">varchar</span></a><span style="color: #FF00FF;">&#40;</span><span style="color: #008080;">15</span><span style="color: #FF00FF;">&#41;</span> <a href="http://dev.mysql.com/doc/refman/%35%2E%31/en/non-typed-operators.html"><span style="color: #CC0099; font-weight: bold;">NOT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=NULL"><span style="color: #9900FF; font-weight: bold;">NULL</span></a><span style="color: #000033;">,</span><br />
&nbsp; <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=PRIMARY%20KEY"><span style="color: #990099; font-weight: bold;">PRIMARY KEY</span></a> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`woe<span style="color: #008080; font-weight: bold;">_</span>id`</span><span style="color: #FF00FF;">&#41;</span><br />
<span style="color: #FF00FF;">&#41;</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ENGINE"><span style="color: #990099; font-weight: bold;">ENGINE</span></a><span style="color: #CC0099;">=</span>MyISAM <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=DEFAULT"><span style="color: #990099; font-weight: bold;">DEFAULT</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=CHARSET"><span style="color: #FF9900; font-weight: bold;">CHARSET</span></a><span style="color: #CC0099;">=</span>utf8<span style="color: #000033;">;</span></div></td></tr></tbody></table></div>
<p>然后，导入之( <strong>geoplanet</strong> 为数据库名)：</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysqlimport <span style="color: #660033;">--ignore-lines</span>=<span style="color: #000000;">1</span> <span style="color: #660033;">--fields-optionally-enclosed-by</span>=<span style="color: #ff0000;">'&quot;'</span> <span style="color: #660033;">--fields-terminated-by</span>=<span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> &nbsp;<span style="color: #660033;">--user</span>=<span style="color: #ff0000;">&quot;root&quot;</span> <span style="color: #660033;">--password</span> <span style="color: #660033;">--local</span> geoplanet places.tsv</div></td></tr></tbody></table></div>
<p>或者直接进mysql:</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=USE"><span style="color: #990099; font-weight: bold;">use</span></a> geoplanet<span style="color: #000033;">;</span><br />
<a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=LOAD"><span style="color: #990099; font-weight: bold;">LOAD</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=DATA"><span style="color: #990099; font-weight: bold;">DATA</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=LOCAL"><span style="color: #990099; font-weight: bold;">LOCAL</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=INFILE"><span style="color: #990099; font-weight: bold;">INFILE</span></a> <span style="color: #008000;">'D:<span style="color: #004000; font-weight: bold;">\\</span>code<span style="color: #004000; font-weight: bold;">\\</span>java<span style="color: #004000; font-weight: bold;">\\</span>geoplanet<span style="color: #008080; font-weight: bold;">_</span>data<span style="color: #008080; font-weight: bold;">_</span>7.6.0<span style="color: #004000; font-weight: bold;">\\</span>Chinaplaces.tsv'</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=INTO"><span style="color: #990099; font-weight: bold;">INTO</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TABLE"><span style="color: #990099; font-weight: bold;">TABLE</span></a> <span style="color: #008000;">`places`</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=FIELDS"><span style="color: #990099; font-weight: bold;">FIELDS</span></a> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=TERMINATED%20BY"><span style="color: #990099; font-weight: bold;">TERMINATED BY</span></a> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\t</span>'</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ENCLOSED%20BY"><span style="color: #990099; font-weight: bold;">ENCLOSED BY</span></a> <span style="color: #008000;">'&quot;'</span> <a href="http://search.mysql.com/search?site=refman-%35%31&amp;q=ESCAPED%20BY"><span style="color: #990099; font-weight: bold;">ESCAPED BY</span></a> <span style="color: #008000;">'<span style="color: #004000; font-weight: bold;">\'</span> LINES TERMINATED BY '</span>\n<span style="color: #008000;">' IGNORE 1 LINES;</span></div></td></tr></tbody></table></div>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">mysql<span style="color: #000000; font-weight: bold;">&gt;</span> use geoplanet<br />
Database changed<br />
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> show tables;<br />
+---------------------+<br />
<span style="color: #000000; font-weight: bold;">|</span> Tables_in_geoplanet <span style="color: #000000; font-weight: bold;">|</span><br />
+---------------------+<br />
<span style="color: #000000; font-weight: bold;">|</span> places &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">|</span><br />
+---------------------+<br />
<span style="color: #000000;">1</span> row <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">set</span> <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.00</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
<br />
mysql<span style="color: #000000; font-weight: bold;">&gt;</span> LOAD DATA LOCAL INFILE <span style="color: #ff0000;">'D:\\code\\java\\geoplanet_data_7.6.0\\Chinaplaces<br />
.tsv'</span> INTO TABLE <span style="color: #000000; font-weight: bold;">`</span>places<span style="color: #000000; font-weight: bold;">`</span> FIELDS TERMINATED BY <span style="color: #ff0000;">'\t'</span> ENCLOSED BY <span style="color: #ff0000;">'&quot;'</span> ESCAPED BY <span style="color: #ff0000;">'<br />
\'</span> LINES TERMINATED BY <span style="color: #ff0000;">'\n'</span> IGNORE <span style="color: #000000;">1</span> LINES;<br />
Query OK, <span style="color: #000000;">10519</span> rows affected <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000;">0.26</span> sec<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
Records: <span style="color: #000000;">10519</span> &nbsp;Deleted: <span style="color: #000000;">0</span> &nbsp;Skipped: <span style="color: #000000;">0</span> &nbsp;Warnings: <span style="color: #000000;">0</span></div></td></tr></tbody></table></div>
<p>然后就是连接mysql啦(java目前我也就写过一个hello world, class写得勉强，见笑了 <img src='http://ihacklog.com/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':smile:' class='wp-smiley' />  )：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ihacklog.java.learn</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Connection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.DriverManager</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Statement</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* MySQL辅助类<br />
&nbsp;* @author HuangYe &lt;荒野无灯&gt;<br />
&nbsp;* @URL http://ihacklog.com<br />
&nbsp;* @see http://dev.mysql.com/doc/refman/5.5/en/connector-j.html<br />
&nbsp;*/</span><br />
<br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> mysql <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> server <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> port <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;3306&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> user <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;geoplanet&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> password <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;geoplanet&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> database <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;geoplanet&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Connection</span></a> conn <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> query <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> mysql<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> server, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> port, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> user, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> password,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> database<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">super</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">=</span> server<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">port</span> <span style="color: #339933;">=</span> port<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">user</span> <span style="color: #339933;">=</span> user<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">password</span> <span style="color: #339933;">=</span> password<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span> <span style="color: #339933;">=</span> database<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// The newInstance() call is a work around for some</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// broken Java implementations</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// handle the error</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* create JDBC connection<br />
&nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp;* @return Connection<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Connection</span></a> connect<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> mysql.<span style="color: #006633;">conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mysql.<span style="color: #006633;">conn</span> <span style="color: #339933;">=</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adrivermanager+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">DriverManager</span></a>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;jdbc:mysql://&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">server</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;:&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">port</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">database</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;?&quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;user=&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">user</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&amp;password=&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">password</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&amp;characterEncoding=utf8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// handle any errors</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SQLException: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SQLState: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">getSQLState</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;VendorError: &quot;</span> <span style="color: #339933;">+</span> ex.<span style="color: #006633;">getErrorCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> mysql.<span style="color: #006633;">conn</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getOneCol<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> query,<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> columnName<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a> stmt <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ResultSet</span></a> rs <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> colString <span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">query</span> <span style="color: #339933;">=</span> query<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; stmt <span style="color: #339933;">=</span> mysql.<span style="color: #006633;">conn</span>.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; colString <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span>columnName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> sqlEx<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// ignore</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">finally</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rs.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ignore</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>stmt <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stmt.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asqlexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">SQLException</span></a> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// ignore</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">return</span> colString<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>然后是天气信息查询类：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ihacklog.java.learn</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//For saving XML file</span><br />
<span style="color: #666666; font-style: italic;">/*import java.io.File;<br />
import java.io.FileOutputStream;*/</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.InputStream</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URL</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.URLConnection</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.DocumentBuilder</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.xml.parsers.DocumentBuilderFactory</span><span style="color: #339933;">;</span><br />
<span style="color: #666666; font-style: italic;">//For saving XML file</span><br />
<span style="color: #666666; font-style: italic;">/*import javax.xml.transform.Transformer;<br />
import javax.xml.transform.TransformerFactory;<br />
import javax.xml.transform.dom.DOMSource;<br />
import javax.xml.transform.stream.StreamResult;*/</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Document</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.NamedNodeMap</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.Node</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.w3c.dom.NodeList</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ihacklog.java.learn.mysql</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* @author 狼の禅 [http://www.cnblogs.com/kingwolfofsky], HuangYe &lt;荒野无灯&gt;<br />
&nbsp;*<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">class</span> GetWeatherInfo <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> mysql db<span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* 天气信息代码<br />
&nbsp; &nbsp; &nbsp;* @see http://developer.yahoo.com/weather/#codes<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> dictionaryStrings <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;龙卷风&quot;</span>, <span style="color: #0000ff;">&quot;热带风暴&quot;</span>, <span style="color: #0000ff;">&quot;飓风&quot;</span>, <span style="color: #0000ff;">&quot;强雷阵雨&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;雷阵雨&quot;</span>, <span style="color: #0000ff;">&quot;小雨加雪&quot;</span>, <span style="color: #0000ff;">&quot;雨加冰雹&quot;</span>, <span style="color: #0000ff;">&quot;雪加冰雹&quot;</span>, <span style="color: #0000ff;">&quot;冰雨&quot;</span>, <span style="color: #0000ff;">&quot;毛毛雨&quot;</span>, <span style="color: #0000ff;">&quot;冻雨&quot;</span>, <span style="color: #0000ff;">&quot;阵雨&quot;</span>, <span style="color: #0000ff;">&quot;阵雨&quot;</span>, <span style="color: #0000ff;">&quot;小雪&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;零星小雪&quot;</span>, <span style="color: #0000ff;">&quot;高吹雪&quot;</span>, <span style="color: #0000ff;">&quot;雪&quot;</span>, <span style="color: #0000ff;">&quot;冰雹&quot;</span>, <span style="color: #0000ff;">&quot;雨夹雪&quot;</span>, <span style="color: #0000ff;">&quot;尘&quot;</span>, <span style="color: #0000ff;">&quot;雾&quot;</span>, <span style="color: #0000ff;">&quot;薄雾&quot;</span>, <span style="color: #0000ff;">&quot;多烟的&quot;</span>, <span style="color: #0000ff;">&quot;大风&quot;</span>, <span style="color: #0000ff;">&quot;有风&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;寒冷&quot;</span>, <span style="color: #0000ff;">&quot;阴天&quot;</span>, <span style="color: #0000ff;">&quot;夜间阴天&quot;</span>, <span style="color: #0000ff;">&quot;白天阴天&quot;</span>, <span style="color: #0000ff;">&quot;夜间多云&quot;</span>, <span style="color: #0000ff;">&quot;白天多云&quot;</span>, <span style="color: #0000ff;">&quot;夜间清亮&quot;</span>, <span style="color: #0000ff;">&quot;晴朗&quot;</span>, <span style="color: #0000ff;">&quot;转晴&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;转晴&quot;</span>, <span style="color: #0000ff;">&quot;雨夹冰雹&quot;</span>, <span style="color: #0000ff;">&quot;热&quot;</span>, <span style="color: #0000ff;">&quot;雷阵雨&quot;</span>, <span style="color: #0000ff;">&quot;雷阵雨&quot;</span>, <span style="color: #0000ff;">&quot;雷阵雨&quot;</span>, <span style="color: #0000ff;">&quot;雷阵雨&quot;</span>, <span style="color: #0000ff;">&quot;大雪&quot;</span>, <span style="color: #0000ff;">&quot;阵雪&quot;</span>, <span style="color: #0000ff;">&quot;大雪&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;多云&quot;</span>, <span style="color: #0000ff;">&quot;雷&quot;</span>, <span style="color: #0000ff;">&quot;阵雪&quot;</span>, <span style="color: #0000ff;">&quot;雷雨&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> GetWeatherInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; GetWeatherInfo.<span style="color: #006633;">db</span><span style="color: #339933;">=</span><span style="color: #000000; font-weight: bold;">new</span> mysql<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span>, <span style="color: #0000ff;">&quot;3306&quot;</span>, <span style="color: #0000ff;">&quot;geoplanet&quot;</span>, <span style="color: #0000ff;">&quot;geoplanet&quot;</span>, <span style="color: #0000ff;">&quot;geoplanet&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getCityWOEID<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> cityName<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> woeid <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; woeid<span style="color: #339933;">=</span>GetWeatherInfo.<span style="color: #006633;">db</span>.<span style="color: #006633;">getOneCol</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT woe_id FROM places WHERE name='&quot;</span><span style="color: #339933;">+</span> cityName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span>,<span style="color: #0000ff;">&quot;woe_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> woeid<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp; &nbsp; &nbsp;* modified by 荒野无灯<br />
&nbsp; &nbsp; &nbsp;* @see http://developer.yahoo.com/weather/ <br />
&nbsp; &nbsp; &nbsp;* @see http://en.wikipedia.org/wiki/WOEID<br />
&nbsp; &nbsp; &nbsp;* @see http://developer.yahoo.com/geo/geoplanet/<br />
&nbsp; &nbsp; &nbsp;* @see http://developer.yahoo.com/geo/geoplanet/data/<br />
&nbsp; &nbsp; &nbsp;* @param cityCode<br />
&nbsp; &nbsp; &nbsp;* @return &nbsp;Document<br />
&nbsp; &nbsp; &nbsp;* @throws IOException<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> getWeatherXML<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> cityCode<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aioexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">IOException</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a> url <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurl+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URL</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://weather.yahooapis.com/forecastrss?w=&quot;</span> <span style="color: #339933;">+</span> cityCode <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;&amp;u=c&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aurlconnection+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">URLConnection</span></a> connection <span style="color: #339933;">=</span> url.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> Doc <span style="color: #339933;">=</span> stringToElement<span style="color: #009900;">&#40;</span>connection.<span style="color: #006633;">getInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> Doc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* 保存获取的天气信息XML文档 */</span><br />
<span style="color: #666666; font-style: italic;">/* &nbsp; &nbsp;private void saveXML(Document Doc, String Path) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; TransformerFactory transFactory = TransformerFactory.newInstance();<br />
&nbsp; &nbsp; &nbsp; &nbsp; Transformer transformer;<br />
&nbsp; &nbsp; &nbsp; &nbsp; try {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transformer = transFactory.newTransformer();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DOMSource domSource = new DOMSource(Doc);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; File file = new File(Path);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream out = new FileOutputStream(file);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StreamResult xmlResult = new StreamResult(out);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transformer.transform(domSource, xmlResult);<br />
&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(&quot;保存文件出错！&quot;);<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; }*/</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">/* 获取天气信息 */</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> getWeather<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> city<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> result <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> cityWOEID <span style="color: #339933;">=</span>getCityWOEID<span style="color: #009900;">&#40;</span>city<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">==</span> cityWOEID <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> doc <span style="color: #339933;">=</span> getWeatherXML<span style="color: #009900;">&#40;</span> cityWOEID <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList nodeList <span style="color: #339933;">=</span> doc.<span style="color: #006633;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;channel&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> nodeList.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node <span style="color: #339933;">=</span> nodeList.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList nodeList1 <span style="color: #339933;">=</span> node.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> j <span style="color: #339933;">&lt;</span> nodeList1.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node1 <span style="color: #339933;">=</span> nodeList1.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>node1.<span style="color: #006633;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NodeList nodeList2 <span style="color: #339933;">=</span> node1.<span style="color: #006633;">getChildNodes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> k <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> k <span style="color: #339933;">&lt;</span> nodeList2.<span style="color: #006633;">getLength</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> k<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node node2 <span style="color: #339933;">=</span> nodeList2.<span style="color: #006633;">item</span><span style="color: #009900;">&#40;</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>node2.<span style="color: #006633;">getNodeName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equalsIgnoreCase</span><span style="color: #009900;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">&quot;yweather:forecast&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NamedNodeMap nodeMap <span style="color: #339933;">=</span> node2.<span style="color: #006633;">getAttributes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node lowNode <span style="color: #339933;">=</span> nodeMap.<span style="color: #006633;">getNamedItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;low&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node highNode <span style="color: #339933;">=</span> nodeMap.<span style="color: #006633;">getNamedItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;high&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Node codeNode <span style="color: #339933;">=</span> nodeMap.<span style="color: #006633;">getNamedItem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> day <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>明天&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>result <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;城市：&quot;</span> <span style="color: #339933;">+</span> city <span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>今天&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; day <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>明天&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result <span style="color: #339933;">=</span> result<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> day<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> dictionaryStrings<span style="color: #009900;">&#91;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainteger+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Integer</span></a><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>codeNode<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">getNodeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>最低温度：&quot;</span> <span style="color: #339933;">+</span> lowNode.<span style="color: #006633;">getNodeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;℃ <span style="color: #000099; font-weight: bold;">\t</span>最高温度：&quot;</span> <span style="color: #339933;">+</span> highNode.<span style="color: #006633;">getNodeValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;℃ &quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//save file JUST for DEBUG</span><br />
<span style="color: #666666; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;saveXML(doc, &quot;D:\\tmp\\Weather.xml&quot;);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> result<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> stringToElement<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Ainputstream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">InputStream</span></a> input<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder db <span style="color: #339933;">=</span> DocumentBuilderFactory.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span style="color: #006633;">newDocumentBuilder</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Adocument+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Document</span></a> doc <span style="color: #339933;">=</span> db.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> doc<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aexception+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Exception</span></a> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>测试：</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br /></div></td><td><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.ihacklog.java.learn</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.ihacklog.java.learn.GetWeatherInfo</span><span style="color: #339933;">;</span><br />
<br />
<br />
<span style="color: #008000; font-style: italic; font-weight: bold;">/**<br />
&nbsp;* @author HuangYe &lt;荒野无灯&gt;<br />
&nbsp;* @URL http://ihacklog.com<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> getWeather <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> arg<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//OK,let's begin the test.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Have fun!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; GetWeatherInfo info <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GetWeatherInfo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> weather <span style="color: #339933;">=</span> info.<span style="color: #006633;">getWeather</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;岳阳市&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>weather<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; weather <span style="color: #339933;">=</span> info.<span style="color: #006633;">getWeather</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;哈尔滨&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>weather<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; weather <span style="color: #339933;">=</span> info.<span style="color: #006633;">getWeather</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;海口市&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>weather<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; weather <span style="color: #339933;">=</span> info.<span style="color: #006633;">getWeather</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;长沙市&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>weather<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; weather <span style="color: #339933;">=</span> info.<span style="color: #006633;">getWeather</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;武汉市&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Asystem+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">System</span></a>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>weather<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p><a href="http://static.ihacklog.com/wp-files/2011/07/java_fetch_weather_report_from_yahoo_api.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/07/java_fetch_weather_report_from_yahoo_api-488x400.png" alt="" title="java_fetch_weather_report_from_yahoo_api" width="488" height="400" class="alignnone size-medium wp-image-4686" /> </a></p>
<p>本文参考文档：<br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fhublog.hubmed.org%2Farchives%2F001888.html" target="_blank">Importing GeoPlanet data into MySQL</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.cnblogs.com%2Fkingwolfofsky%2Farchive%2F2011%2F07%2F23%2F2115098.html" target="_blank">Java获取yahoo天气预报</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fwww.developer.com%2Fjava%2Fdata%2Fjdbc-and-mysql-run-the-programs.html" target="_blank">JDBC and MySQL: Run the Programs</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdeveloper.yahoo.com%2Fgeo%2Fgeoplanet%2Fguide%2F" target="_blank">Yahoo! GeoPlanet Guide</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdeveloper.yahoo.com%2Fgeo%2Fgeoplanet%2Fdata%2F" target="_blank"><br />
Yahoo! GeoPlanet™ Data</a><br />
<a href="http://ihacklog.com/l.php?url=http%3A%2F%2Fdeveloper.yahoo.com%2Fweather%2F%23codes" target="_blank"><br />
Yahoo! Weather RSS Feed docs</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4684";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4684" title="Permanent Link to Java学习之利用yahoo weather api 获取天气预报" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4684</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2008年12月31日  //  <a href="http://ihacklog.com/php/%e6%8b%92%e7%bb%9d%e5%a5%b8%e5%95%86%ef%bc%8c%e8%a3%85%e6%9c%ba%e7%a7%98%e8%af%80%e5%88%86%e4%ba%ab.html" title="拒绝奸商，装机秘诀分享">拒绝奸商，装机秘诀分享</a> (0)</li><li>2011年02月9日  //  <a href="http://ihacklog.com/php/wordpress/upgrade/%e6%89%8b%e5%8a%a8%e6%9b%b4%e6%96%b0wp-3-04-to-3-0-5.html" title="手动更新wp 3.04 to 3.0.5">手动更新wp 3.04 to 3.0.5</a> (4)</li><li>2008年12月22日  //  <a href="http://ihacklog.com/php/framework/thinkphp-%e5%86%85%e9%83%a8%e5%87%bd%e6%95%b0-d%e3%80%81f%e3%80%81s%e3%80%81c%e3%80%81l%e3%80%81a-%e4%bb%96%e4%bb%ac%e7%9a%84%e7%94%a8%e9%80%94%e5%92%8c%e6%89%80%e5%9c%a8%e4%bd%8d%e7%bd%ae.html" title="thinkphp 内部函数 D、F、S、C、L、A 他们的用途和所在位置 ">thinkphp 内部函数 D、F、S、C、L、A 他们的用途和所在位置 </a> (0)</li><li>2009年04月9日  //  <a href="http://ihacklog.com/php/skills/phpjiequzhongwenzifuchuanfangfa.html" title="PHP截取中文字符串方法">PHP截取中文字符串方法</a> (1)</li><li>2010年07月7日  //  <a href="http://ihacklog.com/software/cool_software_share/flash-clock.html" title="分享几个flash时钟">分享几个flash时钟</a> (4)</li><li>2010年04月29日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/gravatar-cache-advanced-method.html" title="Gravatar头像缓存再次修正版">Gravatar头像缓存再次修正版</a> (29)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/java/learning-java-get-weather-report-via-yahoo-weather-api.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>关于C语言的char类型是有符号还是无符号</title>
		<link>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html</link>
		<comments>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html#comments</comments>
		<pubDate>Fri, 24 Jun 2011 07:53:04 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[char]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[signed]]></category>
		<category><![CDATA[unsigned]]></category>

		<guid isPermaLink="false">http://ihacklog.com/?p=4579</guid>
		<description><![CDATA[这是C语言里面比较纠结的一个问题。 有时候这个问题和平台或者编译器有关。 貌似gcc和 Ms cl 都默认是signed 123456789#include&#160; &#160; &#60;stdio.h&#62; #include &#160; &#160;&#60;limits.h&#62; int main&#40;&#41; &#123; &#160; &#160; &#160; &#160; printf&#40;&#34;%d\n&#34;,CHAR_MIN&#41;; &#160; &#...]]></description>
			<content:encoded><![CDATA[<p>这是C语言里面比较纠结的一个问题。<br />
有时候这个问题和平台或者编译器有关。<br />
貌似gcc和 Ms cl 都默认是signed</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include&nbsp; &nbsp; &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &nbsp; &nbsp;&lt;limits.h&gt;</span><br />
<br />
<span style="color: #993333;">int</span><br />
main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>CHAR_MIN<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>输出是 -128 .</p>
<p>其实C标准并没有规定char 应该是unsigned 还是 signed<br />
事实上标准定义了三种类型：char, signed char 和unsigned char<br />
#include
<limits.h>然后检查CHAR_MIN 的值就知道默认的是有符号还是无符号。<br />
若等于0 ，则说明是无符号的，否则，它就是等于SCHAR_MIN (一般是-128）。<br />
我们查看下/usr/include/limits.h<br />
可以发现如下定义：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/* Number of bits in a `char'.&nbsp; */</span><br />
<span style="color: #339933;"># &nbsp;define CHAR_BIT&nbsp; 8</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Minimum and maximum values a `signed char' can hold. &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;define SCHAR_MIN (-128)</span><br />
<span style="color: #339933;"># &nbsp;define SCHAR_MAX 127</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Maximum value an `unsigned char' can hold. &nbsp;(Minimum is 0.) &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;define UCHAR_MAX 255</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* Minimum and maximum values a `char' can hold. &nbsp;*/</span><br />
<span style="color: #339933;"># &nbsp;ifdef __CHAR_UNSIGNED__</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MIN 0</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MAX UCHAR_MAX</span><br />
<span style="color: #339933;"># &nbsp;else</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MIN SCHAR_MIN</span><br />
<span style="color: #339933;"># &nbsp; define CHAR_MAX SCHAR_MAX</span><br />
<span style="color: #339933;"># &nbsp;endif</span></div></td></tr></tbody></table></div>
<p>当然，这个默认行为也是可以通过编译器来改变的，如gcc可通过加<strong>-funsigned-char</strong><br />
来指定其默认为无符号型。<br />
MSVC 可以用<strong>/J</strong>来指定char 默认为无符号型。</p>
<p>本文参考：<br />
<a href="http://stackoverflow.com/questions/2054939/char-is-signed-or-unsigned-by-default">c &#8211; char is signed or unsigned by default &#8211; Stack Overflow</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4579";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4579" title="Permanent Link to 关于C语言的char类型是有符号还是无符号" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4579</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年07月21日  //  <a href="http://ihacklog.com/software/cool_software_share/patch-deadbeef-to-support-gbk-id3-tags-and-add-lyrics-support.html" title="让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示">让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示</a> (4)</li><li>2011年03月18日  //  <a href="http://ihacklog.com/programming/c/filecombine.html" title="文本文件批量合并小工具">文本文件批量合并小工具</a> (15)</li><li>2010年06月1日  //  <a href="http://ihacklog.com/programming/c/tchar-and-char-in-vc.html" title="关于VC中的TCHAR和char">关于VC中的TCHAR和char</a> (0)</li><li>2010年05月26日  //  <a href="http://ihacklog.com/programming/c/cgl-a-cgi-library-in-ansi-c.html" title="CGL库——cgi网页开发好帮手">CGL库——cgi网页开发好帮手</a> (4)</li><li>2010年05月10日  //  <a href="http://ihacklog.com/programming/c/calculate-the-week-of-a-particular-day-a-certain-period.html" title="C语言学习——计算某年某月某日是星期几的函数">C语言学习——计算某年某月某日是星期几的函数</a> (4)</li><li>2010年05月3日  //  <a href="http://ihacklog.com/programming/c/rgb-and-hex-color-convertion.html" title="RGB和十六进制颜色值互转">RGB和十六进制颜色值互转</a> (2)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux中gotoxy函数的一个实现</title>
		<link>http://ihacklog.com/programming/c/gotoxy-in-linux.html</link>
		<comments>http://ihacklog.com/programming/c/gotoxy-in-linux.html#comments</comments>
		<pubDate>Thu, 02 Jun 2011 16:19:08 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[gotoxy]]></category>
		<category><![CDATA[implement]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/programming/c/gotoxy-in-linux.html</guid>
		<description><![CDATA[摘自：Linux: gotoxy() &#8211; C Linux中要移动光标一般要用到ncurses库中的move函数，这里找到一个纯C实现的gotoxy函数： 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465#include &#60;stdio.h&#62; #include &#60;string.h&#...]]></description>
			<content:encoded><![CDATA[<p>摘自：<a href="http://www.daniweb.com/software-development/c/code/216326">Linux: gotoxy() &#8211; C</a></p>
<p>Linux中要移动光标一般要用到ncurses库中的move函数，这里找到一个纯C实现的gotoxy函数：</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<br />
<span style="color: #993333;">void</span> clrscr<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">100</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// A bunch of new lines for now. It's blank, hey!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/putchar.html"><span style="color: #000066;">putchar</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> gotoxy<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> x<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> y<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> essq<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// String variable to hold the escape sequence</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> xstr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Strings to hold the x and y coordinates</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> ystr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Escape sequences must be built with characters</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Convert the screen coordinates to strings<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>xstr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>ystr<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span> y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Build the escape sequence (vertical move)<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; essq<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">'<span style="color: #006699; font-weight: bold;">\0</span>'</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #006699; font-weight: bold;">\033</span>[&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> ystr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Described in man terminfo as vpa=\E[%p1%dd<br />
&nbsp; &nbsp; ** Vertical position absolute<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;d&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Horizontal move<br />
&nbsp; &nbsp; ** Horizontal position absolute<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;<span style="color: #006699; font-weight: bold;">\033</span>[&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> xstr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Described in man terminfo as hpa=\E[%p1%dG</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>essq<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;G&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/*<br />
&nbsp; &nbsp; ** Execute the escape sequence<br />
&nbsp; &nbsp; ** This will move the cursor to x, y<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span> essq<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/*<br />
** Example<br />
*/</span><br />
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; clrscr<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gotoxy<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Coordinates: x = 2; y = 0;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; gotoxy<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">5</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Coordinates: x = 5; y = 5;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4552";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4552" title="Permanent Link to Linux中gotoxy函数的一个实现" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4552</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年10月18日  //  <a href="http://ihacklog.com/linux/myget-the-multi-thread-download-tools-under-linux.html" title="Linux下多线程下载工具Myget原是国人所写">Linux下多线程下载工具Myget原是国人所写</a> (3)</li><li>2011年10月7日  //  <a href="http://ihacklog.com/linux/archlinux/pdnsd-cache-server-setup.html" title="Linux下用pdnsd做dns缓存服务器">Linux下用pdnsd做dns缓存服务器</a> (5)</li><li>2011年07月22日  //  <a href="http://ihacklog.com/linux/archlinux/multi-route-cause-slow-speed.html" title="最近上网很卡——原来是multi-route的缘故">最近上网很卡——原来是multi-route的缘故</a> (2)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/software/font-manager-for-linux.html" title="font manager for linux">font manager for linux</a> (0)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/linux/chinese-chess-for-linux.html" title="Linux下的中国象棋">Linux下的中国象棋</a> (0)</li><li>2011年06月6日  //  <a href="http://ihacklog.com/linux/linux-game-alienarena.html" title="Linux下一款好玩的游戏：alienarena">Linux下一款好玩的游戏：alienarena</a> (4)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/gotoxy-in-linux.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>文本文件批量合并小工具</title>
		<link>http://ihacklog.com/programming/c/filecombine.html</link>
		<comments>http://ihacklog.com/programming/c/filecombine.html#comments</comments>
		<pubDate>Fri, 18 Mar 2011 08:52:55 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>
		<category><![CDATA[txt]]></category>
		<category><![CDATA[合并]]></category>
		<category><![CDATA[文件文件]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4317</guid>
		<description><![CDATA[在深圳时，奔爷有一小说要合并，win下用批处理貌似不好办，Linux下的shell 命令我也不是很熟悉，还是用C语言写一个吧，于是操刀(VIM)开工。因此有了这篇文章。程序很简单，没啥技术含量，拿出来与大家分享。有兴趣的童鞋可以一起来修改它。 Usage:./filecombine prefix start end [combine_name] [fileext] Description:此程序用来合并命名有规律的txt文件为一个文件, 如foo1.txt , foo2.txt ,foo3.txt &#8...]]></description>
			<content:encoded><![CDATA[<p>在深圳时，奔爷有一小说要合并，win下用批处理貌似不好办，Linux下的shell 命令我也不是很熟悉，还是用C语言写一个吧，于是操刀(VIM)开工。因此有了这篇文章。程序很简单，没啥技术含量，拿出来与大家分享。有兴趣的童鞋可以一起来修改它。</p>
<blockquote><p>Usage:./filecombine prefix start end [combine_name] [fileext]</p>
<p>Description:此程序用来合并命名有规律的txt文件为一个文件,<br />
如foo1.txt , foo2.txt ,foo3.txt &#8230;&#8230; foo199.txt , foo200.txt<br />
参数解释：<br />
prefix:        文件共同的前缀(比如上面的 foo)<br />
start:         要合并的文件的开始数字(如1)<br />
end:           要合并的文件的结束数字(如200)<br />
combine_name:  合并后的文件名称(可选)<br />
fileext:       文件后缀(默认为.txt,可选)</p></blockquote>
<p><a href="http://static.ihacklog.com/wp-files/2011/03/filecombine-1.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/03/filecombine-1-550x330.png" alt="" title="filecombine-1" width="550" height="330" class="alignnone size-medium wp-image-4318" /> </a></p>
<p><a href="http://static.ihacklog.com/wp-files/2011/03/filecombine-2.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2011/03/filecombine-2-550x392.png" alt="" title="filecombine-2" width="550" height="392" class="alignnone size-medium wp-image-4319" /> </a></p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">gcc</span> <span style="color: #660033;">-Wall</span> <span style="color: #660033;">-o</span> filecombine <span style="color: #660033;">-O3</span> filecombine.c</div></td></tr></tbody></table></div>
<p>源码如下，可在WIN32或者Linux 32下编译运行。win下access函数是用头文件io.h，linux下没有这个文件，包含于unistd.h中。<br />
附上源码包，里面包含WIN32和LINUX下的可执行文件：<br />
注意：该页面嵌入了下载文件，请访问 <a href="http://ihacklog.com/programming/c/filecombine.html">该页面</a>下载该文件。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Filename: &nbsp;filecombine.c<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp;Description: 此程序用来合并命名有规律的txt文件为一个文件，如foo1.txt , foo2.txt ,foo3.txt ...... foo199.txt , foo200.txt<br />
&nbsp;*&nbsp; &nbsp; &nbsp; 用法：filecombine.exe 文件共同的前缀(比如上面的 foo) 要合并的文件的开始数字(如1) 要合并的文件的结束数字(如200) [合并后的文件名称]<br />
&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (可选) [文件后缀](默认为.txt,可选)<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Version: &nbsp;1.0<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Created: &nbsp;2011年03月01日 21时52分28秒<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Revision: &nbsp;none<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Compiler: &nbsp;gcc<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; Author: &nbsp;荒野无灯 (HuangYeWuDeng), admin#ihacklog.com<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Company: &nbsp;hhtc edu<br />
&nbsp;*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*/</span><br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span><br />
<span style="color: #339933;">#include &lt;string.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/types.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/stat.h&gt;</span><br />
<br />
<span style="color: #339933;">#if defined(WIN32)</span><br />
<span style="color: #339933;">#include &lt;io.h&gt;</span><br />
<span style="color: #339933;">#elif defined(__linux__)</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#endif</span><br />
<br />
<span style="color: #339933;">#define CHUNK_SIZE 512</span><br />
<br />
<span style="color: #993333;">off_t</span> filesize<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #993333;">int</span><br />
main<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>argv<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ************文本文件批量合并小工具************<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error parameter!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Usage:%s prefix start end [combine_name] [fileext]<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,*</span>argv<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Description:此程序用来合并命名有规律的txt文件为一个文件,<span style="color: #000099; font-weight: bold;">\n</span>如foo1.txt , foo2.txt ,foo3.txt ...... foo199.txt , foo200.txt<span style="color: #000099; font-weight: bold;">\n</span>参数解释：<span style="color: #000099; font-weight: bold;">\n</span>prefix: &nbsp; &nbsp; &nbsp; &nbsp;文件共同的前缀(比如上面的 foo) <span style="color: #000099; font-weight: bold;">\n</span>start: &nbsp; &nbsp; &nbsp; &nbsp; 要合并的文件的开始数字(如1)<span style="color: #000099; font-weight: bold;">\n</span>end: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 要合并的文件的结束数字(如200) <span style="color: #000099; font-weight: bold;">\n</span>combine_name: &nbsp;合并后的文件名称(可选) <span style="color: #000099; font-weight: bold;">\n</span>fileext: &nbsp; &nbsp; &nbsp; 文件后缀(默认为.txt,可选)<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;--------------------------------------------------a small tool by 荒野无灯<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;--------------------------------------------------http://www.ihacklog.com<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fp<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fpw<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> fileext<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> combine_filename<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">200</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> renamed_combine_filename<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">300</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>argc<span style="color: #339933;">==</span><span style="color: #0000dd;">6</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>fileext<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>fileext<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;.txt&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>NULL <span style="color: #339933;">!=</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_combined%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_combined%s&quot;</span><span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>access<span style="color: #009900;">&#40;</span>combine_filename<span style="color: #339933;">,</span><span style="color: #208080;">0x00</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> timebuf<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">100</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; time_t t<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span style="color: #000066;">time</span></a> <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>t<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strftime.html"><span style="color: #000066;">strftime</span></a><span style="color: #009900;">&#40;</span>timebuf<span style="color: #339933;">,</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span>timebuf<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%Y-%m-%d-%H%M%S&quot;</span><span style="color: #339933;">,</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/localtime.html"><span style="color: #000066;">localtime</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>t<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%s_%s&quot;</span><span style="color: #339933;">,</span>timebuf<span style="color: #339933;">,</span>combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;注意：文件%s已经存在，合并后的文件名为：%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>combine_filename<span style="color: #339933;">,</span>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span>combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; fpw<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html"><span style="color: #000066;">fopen</span></a><span style="color: #009900;">&#40;</span>renamed_combine_filename<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;ab&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fpw<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;create file %s failed!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> start_num<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/atoi.html"><span style="color: #000066;">atoi</span></a><span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> end_num<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/atoi.html"><span style="color: #000066;">atoi</span></a><span style="color: #009900;">&#40;</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span>start_num<span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span>end_num<span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> fname<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">200</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcpy.html"><span style="color: #000066;">strcpy</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>argv<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> tmp<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/sprintf.html"><span style="color: #000066;">sprintf</span></a><span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;%d&quot;</span><span style="color: #339933;">,</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcat.html"><span style="color: #000066;">strcat</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span>fileext<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; fp<span style="color: #339933;">=</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/fopen.html"><span style="color: #000066;">fopen</span></a><span style="color: #009900;">&#40;</span>fname<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;rb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>fp<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Error:<span style="color: #000099; font-weight: bold;">\n</span>open file %s failed!perhaps file does not exists.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;正在添加文件: &nbsp;%s ......<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">char</span> data<span style="color: #009900;">&#91;</span>CHUNK_SIZE<span style="color: #339933;">+</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> file_size<span style="color: #339933;">=</span>filesize<span style="color: #009900;">&#40;</span>fname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> toread<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>file_size<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toread<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span> file_size<span style="color: #339933;">&gt;</span>CHUNK_SIZE <span style="color: #009900;">&#41;</span><span style="color: #339933;">?</span>CHUNK_SIZE<span style="color: #339933;">:</span>file_size<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fread.html"><span style="color: #000066;">fread</span></a><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span>toread<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>fp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fwrite.html"><span style="color: #000066;">fwrite</span></a><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span>toread<span style="color: #339933;">,</span><span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>fpw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; file_size<span style="color: #339933;">-=</span>toread<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html"><span style="color: #000066;">fclose</span></a><span style="color: #009900;">&#40;</span>fp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fclose.html"><span style="color: #000066;">fclose</span></a><span style="color: #009900;">&#40;</span>fpw<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><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>renamed_combine_filename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<br />
<span style="color: #993333;">off_t</span><br />
filesize<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>filename<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">struct</span> stat s<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #0000dd;">0</span> <span style="color: #339933;">!=</span> stat<span style="color: #009900;">&#40;</span>filename<span style="color: #339933;">,&amp;</span>s<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> s.<span style="color: #202020;">st_size</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4317";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4317" title="Permanent Link to 文本文件批量合并小工具" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4317</a>
            </p>
             </div><h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011年07月21日  //  <a href="http://ihacklog.com/software/cool_software_share/patch-deadbeef-to-support-gbk-id3-tags-and-add-lyrics-support.html" title="让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示">让Deadbeef支持GBK标签,彻底解决中文乱码问题，增加歌词显示</a> (4)</li><li>2011年06月24日  //  <a href="http://ihacklog.com/programming/c/char-is-signed-or-unsigned-by-default.html" title="关于C语言的char类型是有符号还是无符号">关于C语言的char类型是有符号还是无符号</a> (0)</li><li>2010年05月26日  //  <a href="http://ihacklog.com/programming/c/cgl-a-cgi-library-in-ansi-c.html" title="CGL库——cgi网页开发好帮手">CGL库——cgi网页开发好帮手</a> (4)</li><li>2010年05月10日  //  <a href="http://ihacklog.com/programming/c/calculate-the-week-of-a-particular-day-a-certain-period.html" title="C语言学习——计算某年某月某日是星期几的函数">C语言学习——计算某年某月某日是星期几的函数</a> (4)</li><li>2010年05月3日  //  <a href="http://ihacklog.com/programming/c/rgb-and-hex-color-convertion.html" title="RGB和十六进制颜色值互转">RGB和十六进制颜色值互转</a> (2)</li><li>2010年04月25日  //  <a href="http://ihacklog.com/programming/c/differences-between-c-and-cpp.html" title="读书笔记：C与C++的区别">读书笔记：C与C++的区别</a> (7)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/filecombine.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>删除.svn目录的方法</title>
		<link>http://ihacklog.com/programming/remove-svn-folders.html</link>
		<comments>http://ihacklog.com/programming/remove-svn-folders.html#comments</comments>
		<pubDate>Sat, 01 Jan 2011 09:49:44 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4293</guid>
		<description><![CDATA[即.svn目录的删除。 1.Linux 平台 1find . -type d -name &#34;.svn&#34;&#124;xargs rm -rf 或者 1find . -type d -iname &#34;.svn&#34; -exec rm -rf &#123;&#125; \; 2.Windows平台 Jon Galloway提供了一段注册表代码，可以将”Delete SVN Folders”命名增加到资源管理器的右键上，这样，鼠标点两下就能把选中目录下的所有.svn目录干掉了。Works...]]></description>
			<content:encoded><![CDATA[<p>即.svn目录的删除。<br />
1.Linux 平台</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> d <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;.svn&quot;</span><span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span></div></td></tr></tbody></table></div>
<p>或者</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> d <span style="color: #660033;">-iname</span> <span style="color: #ff0000;">&quot;.svn&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #7a0874; font-weight: bold;">&#125;</span> \;</div></td></tr></tbody></table></div>
<p>2.Windows平台<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=weblogs.asp.net/jgalloway/archive/2007/02/24/shell-command-remove-svn-folders.aspx">Jon Galloway</a>提供了一段注册表代码，可以将”Delete SVN Folders”命名增加到资源管理器的右键上，这样，鼠标点两下就能把选中目录下的所有.svn目录干掉了。Works just great!</p>
<div class="codecolorer-container ini default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="ini codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Windows Registry Editor Version <span style="">5.00</span> <br />
<br />
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN<span style="">&#93;</span></span><br />
@<span style="color: #000066; font-weight:bold;">=</span><span style="color: #933;">&quot;Delete SVN Folders&quot;</span> <br />
<br />
<span style="color: #000066; font-weight:bold;"><span style="">&#91;</span>HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command<span style="">&#93;</span></span><br />
@<span style="color: #000066; font-weight:bold;">=</span><span style="color: #933;">&quot;cmd.exe /c \&quot;</span>TITLE Removing SVN Folders in %1 &amp;&amp; COLOR 9A &amp;&amp; FOR /r \<span style="color: #933;">&quot;%1\&quot;</span> %%f IN <span style="">&#40;</span>.svn<span style="">&#41;</span> DO RD /s /q \<span style="color: #933;">&quot;%%f\&quot;</span> \<span style="color: #933;">&quot;&quot;</span></div></td></tr></tbody></table></div>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4293";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4293" title="Permanent Link to 删除.svn目录的方法" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4293</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2009年04月9日  //  <a href="http://ihacklog.com/php/discuz/discuz70luntanshouyebeijingyinlechajianbyhuangyewudengdaiyinlehexiuzhengban2009nian03yue13rigengxin.html" title="discuz7.0论坛首页背景音乐插件[By荒野无灯]（带音乐盒，修正版）2009年03月13日更新">discuz7.0论坛首页背景音乐插件[By荒野无灯]（带音乐盒，修正版）2009年03月13日更新</a> (0)</li><li>2010年06月8日  //  <a href="http://ihacklog.com/net/powerful-netsh.html" title="windows下的功能强大的netsh命令">windows下的功能强大的netsh命令</a> (0)</li><li>2011年10月14日  //  <a href="http://ihacklog.com/programming/c/blowfish-cipher.html" title="BlowFish 加密算法Bcrypt">BlowFish 加密算法Bcrypt</a> (2)</li><li>2008年12月22日  //  <a href="http://ihacklog.com/server/nginx%e7%9a%84rewrite%e8%ae%be%e7%bd%ae%e5%8f%8a%e7%a4%ba%e4%be%8b.html" title="Nginx的Rewrite设置及示例">Nginx的Rewrite设置及示例</a> (0)</li><li>2009年09月28日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/replace-wordpress-default-search-with-google-custom-search.html" title="用google搜索替换wordpress的默认搜索">用google搜索替换wordpress的默认搜索</a> (7)</li><li>2009年10月24日  //  <a href="http://ihacklog.com/linux/ubuntu-desktop/update-graphic-driver-and-play-games-under-ubuntu.html" title="更换显卡驱动，Ubuntu也玩游戏">更换显卡驱动，Ubuntu也玩游戏</a> (9)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/remove-svn-folders.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>avast病毒库更新程序的改进</title>
		<link>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html</link>
		<comments>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html#comments</comments>
		<pubDate>Fri, 26 Nov 2010 01:12:26 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4250</guid>
		<description><![CDATA[avast病毒库更新程序为avast-update ，这个东东实际上是调用了wget 来下载病毒库的，但是运行以后没有任何提示，病毒库到底有没有在下载呢？不清楚。于是写了个东东来以便可以直观的看到是否在下载以及下载速度的快慢（根据旋转速度）。 /opt/avast4workstation-1.3.0/bin/avast-update 是 avast-update 的路径。 这个小程序主要用到了LINUX内核的 inotify 来监控文件变化。 TODO： 增加超时检测（超时自动退出） 增加更新完成后自动退...]]></description>
			<content:encoded><![CDATA[<p>avast病毒库更新程序为avast-update ，这个东东实际上是调用了wget 来下载病毒库的，但是运行以后没有任何提示，病毒库到底有没有在下载呢？不清楚。于是写了个东东来以便可以直观的看到是否在下载以及下载速度的快慢（根据旋转速度）。<br />
<strong>/opt/avast4workstation-1.3.0/bin/avast-update</strong> 是 avast-update 的路径。</p>
<p>这个小程序主要用到了LINUX内核的 inotify 来监控文件变化。</p>
<p>TODO：<br />
增加超时检测（超时自动退出）<br />
增加更新完成后自动退出（这一点应该简单）</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">/*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Filename: &nbsp;avast-watch.c<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp;Description: &nbsp;监视avast病毒库更新<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Version: &nbsp;1.0<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Created: &nbsp;2010年10月13日 13时35分28秒<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Revision: &nbsp;none<br />
&nbsp;* &nbsp; &nbsp; &nbsp; Compiler: &nbsp;gcc<br />
&nbsp;*<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; Author: &nbsp;荒野无灯 (HuangYeWuDeng), admin@ihacklog.com<br />
&nbsp;* &nbsp; &nbsp; &nbsp; &nbsp;Company: &nbsp;hhtc edu<br />
&nbsp;*<br />
&nbsp;* =====================================================================================<br />
&nbsp;*/</span><br />
<br />
<span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #339933;">#include &lt;unistd.h&gt;</span><br />
<span style="color: #339933;">#include &lt;time.h&gt;</span><br />
<span style="color: #339933;">#include &lt;sys/inotify.h&gt;</span><br />
<span style="color: #339933;">#define MAX_BUF_SIZE 4096</span><br />
<br />
<br />
<br />
<span style="color: #808080; font-style: italic;">/* &nbsp;<br />
struct inotify_event {<br />
int wd; // watch descriptor<br />
uint32_t mask; // mask of events<br />
uint32_t cookie; // unique cookie associating related events ( form rename(2) )<br />
uint32_t len; // size of name field<br />
char name[]; // optional null-terminated name<br />
}<br />
*/</span><br />
<br />
<br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// fd : file discriptor</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// wp &nbsp;watch discriptor</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> fd<span style="color: #339933;">,</span> wd<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> len<span style="color: #339933;">,</span> index<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">4096</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">struct</span> inotify_event <span style="color: #339933;">*</span>event<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>path <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;/home/hacklog/.avast&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>prog<span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;/opt/avast4workstation-1.3.0/bin/avast-update&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> progress <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">char</span> indicator<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;-<span style="color: #000099; font-weight: bold;">\\</span>|/&quot;</span><span style="color: #339933;">;</span><br />
<br />
<br />
&nbsp; &nbsp; FILE <span style="color: #339933;">*</span>fp<span style="color: #339933;">=</span>popen<span style="color: #009900;">&#40;</span>prog<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; fd <span style="color: #339933;">=</span> inotify_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>fd <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Failed to initialize inotify.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; wd <span style="color: #339933;">=</span> inotify_add_watch<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span> path<span style="color: #339933;">,</span> IN_CLOSE_WRITE <span style="color: #339933;">|</span> IN_CREATE <span style="color: #339933;">|</span> IN_MODIFY<span style="color: #339933;">|</span> IN_DELETE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>wd <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Can't add watch for %s&quot;</span><span style="color: #339933;">,</span> path<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 当没有事件发生时，fd 处于阻塞状态，read阻塞直到有一个事件发生.</span><br />
&nbsp; &nbsp; time_t start_time<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; time_t end_time<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>len <span style="color: #339933;">=</span> read<span style="color: #009900;">&#40;</span>fd<span style="color: #339933;">,</span> buffer<span style="color: #339933;">,</span> MAX_BUF_SIZE<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; index <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>index <span style="color: #339933;">&lt;</span> len<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> inotify_event <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#40;</span>buffer <span style="color: #339933;">+</span> index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// 如果不是我们添加的对.avast目录的监视，就继续 （因为我们可能添加了多个监视的，每个监视都有一个 wd (watch discriptor )</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>wd <span style="color: #339933;">!=</span> wd<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (event-&gt;mask &amp; IN_OPEN)</span><br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;file %s is opened.\n&quot;, event-&gt;name);</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_CREATE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> <a href="http://www.opengroup.org/onlinepubs/009695399/functions/strcmp.html"><span style="color: #000066;">strcmp</span></a><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>name<span style="color: #339933;">,</span><span style="color: #ff0000;">&quot;400.vps.new.md5&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/time.html"><span style="color: #000066;">time</span></a><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span>start_time<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is created.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_MODIFY<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
<span style="color: #666666; font-style: italic;">//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf(&quot;file %s is modified.\n&quot;, event-&gt;name);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot; %c&quot;</span><span style="color: #339933;">,</span>indicator<span style="color: #009900;">&#91;</span>progress<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/fflush.html"><span style="color: #000066;">fflush</span></a><span style="color: #009900;">&#40;</span>stdout<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\b</span><span style="color: #000099; font-weight: bold;">\b</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; progress<span style="color: #339933;">=</span><span style="color: #009900;">&#40;</span> progress <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span> &nbsp;<span style="color: #339933;">%</span> <span style="color: #0000dd;">4</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_CLOSE_WRITE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is closed for write.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">-&gt;</span>mask <span style="color: #339933;">&amp;</span> IN_DELETE<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;file %s is deleted.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> event<span style="color: #339933;">-&gt;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// sizeof(struct inotify_event)+len 为每个inotify_event 的长度</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index <span style="color: #339933;">+=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> inotify_event<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> event<span style="color: #339933;">-&gt;</span>len<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>IBM DW：<a href="http://static.ihacklog.com/wp-files/2010/11/avast-update-watch.png" class="tinybox2"  onclick="TINY.box.show({image:this.href,boxid:'frameless',animate:true,fixed:false});return false;"  title="Click to enlarge（点击查看大图）" ><img src="http://static.ihacklog.com/wp-files/2010/11/avast-update-watch-550x384.png" alt="" title="avast-update-watch" width="550" height="384" class="alignnone size-medium wp-image-4251" /> </a></p>
<p>参考文档：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.ibm.com/developerworks/cn/linux/l-inotifynew/">inotify &#8212; Linux 2.6 内核中的文件系统变化通知机制</a><br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=blog.csdn.net/soulxu/archive/2008/10/19/3087292.aspx">linux开发 &#8212; 文件监控 inotify</a><br />
扩展阅读：<br />
<a target="_blank" href="http://www.ihacklog.com/l.php?url=www.bitscn.com/plus/view.phporz__aid=158600">linux inotify+rsync+ssh数据时时同步</a></p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4250";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4250" title="Permanent Link to avast病毒库更新程序的改进" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4250</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2009年06月3日  //  <a href="http://ihacklog.com/php/wordpress/plugins/wploginlogger.html" title="wp login logger">wp login logger</a> (4)</li><li>2011年08月2日  //  <a href="http://ihacklog.com/linux/archlinux/dropping-tcp_wrappers-support.html" title="Archlinux移除了对tcp_wrappers的支持">Archlinux移除了对tcp_wrappers的支持</a> (1)</li><li>2011年10月4日  //  <a href="http://ihacklog.com/php/wordpress/wpskills/how-to-check-a-script-has-been-registerd-or-not-in-wordpress.html" title="WP中检测脚本是否已经注册的方法">WP中检测脚本是否已经注册的方法</a> (0)</li><li>2009年12月6日  //  <a href="http://ihacklog.com/memory/hacklog/blogshow-a-blog-show-system-based-on-php.html" title="最近折腾blogshow">最近折腾blogshow</a> (8)</li><li>2011年06月23日  //  <a href="http://ihacklog.com/linux/archlinux/install-virtualbox-under-archlinux.html" title="Archlinux 下安装virtualbox">Archlinux 下安装virtualbox</a> (1)</li><li>2011年11月16日  //  <a href="http://ihacklog.com/software/tips/opnet-14-5-vs2008-win7-trouble-and-the-solution.html" title="opnet 14.5 + VS2008 + win7＝麻烦">opnet 14.5 + VS2008 + win7＝麻烦</a> (6)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/avast-update-custom-written-in-c.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于递归与迭代</title>
		<link>http://ihacklog.com/programming/c/recursive-and-iterative.html</link>
		<comments>http://ihacklog.com/programming/c/recursive-and-iterative.html#comments</comments>
		<pubDate>Wed, 24 Nov 2010 06:08:52 +0000</pubDate>
		<dc:creator>荒野无灯</dc:creator>
				<category><![CDATA[c/c++]]></category>

		<guid isPermaLink="false">http://www.ihacklog.com/?p=4240</guid>
		<description><![CDATA[这篇文章是由一道省二计算机等级考试的题目引发的。 那天寝室一同学的同学考省二，发短信过来求救，要我给他算一个题目。 题目简单描述如下： f（0）=f（1）=1 f（2）=0 f(n)=f(n-1)-2*f(n-2)+f(n-3) 求当n2) f(n)的最大值是多少。 由于时间非常紧，那同学快下考了，首先我便用了简单的递归实现f(n)函数。 1234567891011121314151617181920212223242526272829303132333435363738#include &#60;std...]]></description>
			<content:encoded><![CDATA[<p>这篇文章是由一道省二计算机等级考试的题目引发的。<br />
那天寝室一同学的同学考省二，发短信过来求救，要我给他算一个题目。<br />
题目简单描述如下：</p>
<blockquote><p>f（0）=f（1）=1<br />
f（2）=0<br />
f(n)=f(n-1)-2*f(n-2)+f(n-3)<br />
求当n<= 50 时 (n>2) f(n)的最大值是多少。</p></blockquote>
<p>由于时间非常紧，那同学快下考了，首先我便用了简单的<strong>递归</strong>实现f(n)函数。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> i<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> max<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;</span><span style="color: #0000dd;">51</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//printf(&quot;now :%d\n&quot;,i);</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> max <span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max<span style="color: #339933;">=</span>f<span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>max <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>f<span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>一运行，发现老半天不出结果，调试一下发现，算到n等于30多的时候就变得非常慢了，CPU占用达50%以上。很明显，这个函数不能用递归来实现。因为递归实际上<strong>效率是非常低</strong>的，而且，就这个题目而言，运算量非常大。<br />
于是赶紧修改了一下f(n) 函数，转而用<strong>迭代</strong>来实现。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:500px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333;">int</span> f<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> n<span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> a<span style="color: #339933;">,</span>b<span style="color: #339933;">,</span>c<span style="color: #339933;">,</span>d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; a<span style="color: #339933;">=</span>b<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; c<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">case</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333;">int</span> t<span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">&gt;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// a b c d</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d<span style="color: #339933;">=</span>c<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>b <span style="color: #339933;">+</span> a<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t<span style="color: #339933;">=</span>d<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>c<span style="color: #339933;">+</span>b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a<span style="color: #339933;">=</span>b<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b<span style="color: #339933;">=</span>c<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c<span style="color: #339933;">=</span>d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d<span style="color: #339933;">=</span>t<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n<span style="color: #339933;">--;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> d<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>这样再运行，结果马上出来了： 598325<br />
速给那同学把答案发去了。</p>
<p>后来，又再网上找到另一种更简便的方法，用一个数组来存储每次运算的结果，这样，后面的运行就可以直接利用前面运算得出的结果，而不要重新计算。从效率来说，这个更高一些。</p>
<div class="codecolorer-container c default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="c codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339933;">#include &lt;stdio.h&gt;</span><br />
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><br />
<span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333;">int</span> arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">51</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>i<span style="color: #339933;">,</span>num_max<span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000dd;">1</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>i<span style="color: #339933;">&lt;=</span><span style="color: #0000dd;">50</span><span style="color: #339933;">;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #339933;">*</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">+</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #339933;">-</span><span style="color: #0000dd;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>num_max<span style="color: #339933;">&lt;</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num_max<span style="color: #339933;">=</span>arr<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;max=%d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>num_max<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span></div></td></tr></tbody></table></div>
<p>看来我还是把问题想复杂了，有时候，最简单的方法就是最好的方法。简单就是美。</p>

<script type="text/javascript"> 
var cur_host=top.location.hostname;
var huangye_host="ihacklog.com";
if ( huangye_host != cur_host) 
{
	var cur_url=top.location.href;
	//top.location.href = cur_url.replace(cur_host,huangye_host);
	top.location.href = "http://ihacklog.com/?p=4240";
}
</script> 			
<div class='sub'><h4>喜欢这篇文章吗?</h4><p>请订阅本站 <a class="feed" style="font-family:Consolas,'DejaVu Sans Mono',monospace,'Comic Sans MS',Monaco;font-size:14px;" href="http://ihacklog.com/feed" onclick="prompt(&#39;URL:&#39;, this.href); return false;">RSS feed</a> 或<a style="display:inline-block;width:90px;margin-bottom:-8px;" target="_blank" href="http://list.qq.com/cgi-bin/qf_invite?id=5899d1ae341c4fb741adf6648000fbaf3ef47b98e2a163da"><img border="0" alt="填写您的邮件地址，订阅我们的精彩内容：" src="http://rescdn.list.qq.com/zh_CN/htmledition/images/qunfa/manage/picMode_dark_s.png" /></a></p></div>
                <!-- 版权声明开始 -->
   		<div id="permissions">
		作者：<a href="http://ihacklog.com">荒野无灯</a><br/>
		出处：<span style="color: #333300;"><a target="_blank"  href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span><br/>
                <!-- 版权声明结束 -->
                <!-- 协议声明开始 -->
            <p>
            <strong>声明:</strong> 本站遵循 <span style="color: #ff0000;"><a href="http://creativecommons.org/licenses/by-nc-sa/2.5/cn/" target="_blank"> 署名-非商业性使用-相同方式共享 2.5</a> </span>共享协议. 转载请注明转自<span style="color: #333300;"><a target="_blank" href="http://ihacklog.com"><strong>Hacklog</strong>【荒野无灯weblog】</a></span>
            </p>
                <!-- 协议声明结束 -->
                
             <p>
             本文链接:  <a  target="_blank"  href="http://ihacklog.com/?p=4240" title="Permanent Link to 关于递归与迭代" onclick="prompt(&#39;URL:&#39;, this.href); return false;">http://ihacklog.com/?p=4240</a>
            </p>
             </div><h3  class="related_post_title">随机日志</h3><ul class="related_post"><li>2009年04月7日  //  <a href="http://ihacklog.com/php/xiuyixiashangweichuludexiaocmsngcms.html" title="秀一下尚未出炉的小CMS：NGCMS">秀一下尚未出炉的小CMS：NGCMS</a> (0)</li><li>2011年12月19日  //  <a href="http://ihacklog.com/xml_html_css_js_ajax/google-webfonts.html" title="Google Web fonts 试用">Google Web fonts 试用</a> (12)</li><li>2008年12月7日  //  <a href="http://ihacklog.com/php/skills/securing-forms-random-image.html" title="Securing Forms &#8211; Random Image">Securing Forms &#8211; Random Image</a> (0)</li><li>2011年05月28日  //  <a href="http://ihacklog.com/software/scripts/speed-up-your-firefox-under-linux.html" title="让你的firefox飞起来">让你的firefox飞起来</a> (0)</li><li>2011年09月30日  //  <a href="http://ihacklog.com/software/development-tool/sublime-text-2-tips-and-tricks.html" title="Sublime Text 2 小技巧和小诀窍">Sublime Text 2 小技巧和小诀窍</a> (7)</li><li>2010年07月17日  //  <a href="http://ihacklog.com/software/cool_software_share/atnow.html" title="AtNow——让程序延时执行">AtNow——让程序延时执行</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://ihacklog.com/programming/c/recursive-and-iterative.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

