📄 check__sum_8cpp.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>check_sum: check_sum.cpp文件参考</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- 制作者 Doxygen 1.3.9.1 --><div class="qindex"><a class="qindex" href="index.html">首页</a> | <a class="qindex" href="namespaces.html">名字空间列表</a> | <a class="qindex" href="files.html">文件列表</a> | <a class="qindex" href="globals.html">文件成员</a></div><h1>check_sum.cpp文件参考</h1>定义控制台应用程序的入口点。 <a href="#_details">更多...</a><p><code>#include "<a class="el" href="stdafx_8h-source.html">stdafx.h</a>"</code><br><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr><tr><td colspan="2"><br><h2>Namespaces</h2></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">namespace </td><td class="memItemRight" valign="bottom"><a class="el" href="namespacestd.html">std</a></td></tr><tr><td colspan="2"><br><h2>函数</h2></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="check__sum_8cpp.html#a0">_tmain</a> (int argc, _TCHAR *argv[])</td></tr></table><hr><a name="_details"></a><h2>详细描述</h2>定义控制台应用程序的入口点。 <p><hr><h2>函数文档</h2><a class="anchor" name="a0" doxytag="check_sum.cpp::_tmain"></a><p><table class="mdTable" width="100%" cellpadding="2" cellspacing="0"> <tr> <td class="mdRow"> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td class="md" nowrap valign="top">int _tmain </td> <td class="md" valign="top">( </td> <td class="md" nowrap valign="top">int </td> <td class="mdname" nowrap> <em>argc</em>, </td> </tr> <tr> <td class="md" nowrap align="right"></td> <td class="md"></td> <td class="md" nowrap>_TCHAR * </td> <td class="mdname" nowrap> <em>argv</em>[]</td> </tr> <tr> <td class="md"></td> <td class="md">) </td> <td class="md" colspan="2"></td> </tr> </table> </td> </tr></table><table cellspacing="5" cellpadding="0" border="0"> <tr> <td> </td> <td><p><div class="fragment"><pre class="fragment">00012 {00013 <span class="keywordflow">if</span>(argc!=2) <span class="comment">//输入参数出错,返回失败</span>00014 {00015 cout<<<span class="stringliteral">"USAGE:inouttest file"</span><<endl;00016 00017 <span class="keywordflow">return</span> 1;00018 }00019 ifstream in(argv[1]);00020 <span class="keywordflow">if</span>(!in) <span class="comment">//无法打开输入文件,返回失败</span>00021 {00022 cout<<<span class="stringliteral">"cannot open file"</span>;00023 <span class="keywordflow">return</span> 1;00024 00025 }00026 <span class="keywordtype">int</span> sumall=0; <span class="comment">// 数据总和</span>00027 <span class="keywordtype">char</span> temp;00028 <span class="keywordtype">int</span> n=0;00029 <span class="keywordtype">int</span> sum=0; <span class="comment">// 每次读取的四个char类型数据,折合为二进制数的和</span>00030 <span class="keywordflow">while</span>(!in.eof()) <span class="comment">//按16位读取数据,</span>00031 {00032 in.get(temp);00033 <span class="keywordflow">if</span>(n<4) <span class="comment">//每次读取四个char型数据(恰好为4个4位二进制数)</span>00034 {00035 n++;00036 <span class="keywordflow">if</span>(temp>=<span class="charliteral">'A'</span>&&temp<=<span class="charliteral">'F'</span>) <span class="comment">//由于输入文件为.txt格式,需要把数据由char转换为16进制数</span>00037 {00038 temp=temp-55;00039 sum=16*sum+temp;00040 }00041 <span class="keywordflow">else</span> 00042 <span class="keywordflow">if</span>(temp>=<span class="charliteral">'0'</span>&&temp<=<span class="charliteral">'9'</span>)00043 {00044 temp=temp-48;00045 sum=16*sum+temp;00046 }00047 <span class="keywordflow">else</span>00048 n--;00049 }00050 <span class="keywordflow">if</span>(n==4) <span class="comment">//读完4个char,将其加入总和中</span>00051 <span class="comment">//文件为偶数个字节时,亦直接调用此部分</span>00052 {00053 n=0;00054 sumall=sumall+sum;00055 sum=0;00056 }00057 00058 00059 }00060 <span class="keywordflow">if</span>(n<4&&n>0) <span class="comment">//文件为奇数个字节,则将其作为高八位,也加入总和中</span>00061 {00062 sum=sum*16*16;00063 sumall=sumall+sum;00064 }00065 cout<<endl;00066 cout<<<span class="stringliteral">"数据总和:"</span><<sumall<<endl; <span class="comment">//输出总和</span>00067 <span class="keywordflow">while</span>(sumall>>16) <span class="comment">//将总和折叠为16位数据(参见RFC1071中算法)</span>00068 sumall=(sumall>>16)+(sumall&0xffff);00069 00070 cout<<<span class="stringliteral">"校验和:"</span><<hex<<(<span class="keywordtype">unsigned</span> short)~sumall<<endl;<span class="comment">//输出校验和:折叠后总和的反码</span>00071 00072 <span class="keywordflow">return</span> 0;00073 }</pre></div><p> </td> </tr></table><hr size="1"><address style="align: right;"><small>Generated at Mon Oct 25 14:34:38 2004 for check_sum by <a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -