2、查看下面程序的输出,解释为什么会有这样的输出。
#include <iostream> #include <fstream> using namespace std; int main( ) { unsigned char a[] = {0x32,0x30,0x31,0x35,0xA3,0xAC,0xCE,0xD2,0xC3,0xC7,0xB3,0xD4, 0xC1,0xCB,0xB5,0xDA,0xD2,0xBB,0xBF,0xDA,0xF3,0xA6,0xD0,0xB7}; ofstream outfile("f3.dat",ios::out|ios::binary); outfile.write((char*)a, sizeof(a)); outfile.close(); return 0; }
因为ox开头的代码代表汉字。
3、阅读下面的程序,指出其功能,体会seekg()、tellg()等函数的功能及其用法
(1)
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include<iostream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <fstream></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> * filename = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"a.txt"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main () { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span> l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file.seekg (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, ios::end); m = file.tellg(); file.close(); <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"size of "</span> << filename; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" is "</span> << (m-l) << <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" bytes.\n"</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li></ul>
(2)
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#include <fstream></span> using namespace std; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main (){ long <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pos</span>; ofstream outfile; outfile.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">open</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"test.txt"</span>); outfile.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">write</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"This is an apple"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">16</span>); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pos</span>=outfile.tellp(); outfile.seekp (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">pos</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">7</span>); outfile.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">write</span> (<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">" sam"</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>); outfile.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">close</span>(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
(3)
<code class="hljs cpp has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream> </span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <fstream> </span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { fstream outfile,infile; outfile.open(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"data.txt"</span>,ios::out); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>;i<<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">26</span>;i++) outfile<<(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span>)(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'A'</span>+i); outfile.close(); infile.open(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"data.txt"</span>,ios::in); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> ch; infile.seekg(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">6</span>,ios::beg); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(infile.get(ch)) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<ch; infile.seekg(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>,ios::beg); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(infile.get(ch)) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<ch; infile.seekg(-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>,ios::end); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(infile.get(ch)) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<ch; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span><<endl; infile.close(); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code>