15 Week "C + + language Basics" program reading--random reading and writing of binary files and files (1)

Source: Internet
Author: User

1, read and run the following two programs, respectively, with Notepad and binary file reader (please download binary viewer and other programs, or DOS in the debug program, and Baidu usage). View its contents and understand how the file is stored.

(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-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <cstdlib></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 () {<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;"    >int</span> A; Ofstream outfile (<span class= "hljs-string" style= "Color:rgb (0, 136, 0); box-sizing:border-box;"    > "F1.dat" </span>,ios::out); <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; "  >if</span> (!outfile) {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing: Border-box; " >cerr</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "Open error!"        </span><<endl; <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >exit</span> (<span class= "Hljs-number" style= "Color:rgb (0, 102, 102); Box-sizing:border-box; "    >1</span>); } <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; "    >cin</span>>>a;    outfile<<a<<endl;       Outfile.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><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&Gt;<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><li style= "Box-sizing:border-box; padding:0px 5px;" >17</li><li style= "Box-sizing:border-box; padding:0px 5px; " >18</li></ul>


(2)

<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-preprocessor" style= "Color:rgb (68, 68, 68); Box-sizing:border-box; " > #include <cstdlib></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 () {<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;"    >int</span> A; Ofstream outfile (<span class= "hljs-string" style= "Color:rgb (0, 136, 0); box-sizing:border-box;"    > "F2.dat" </span>,ios::out|ios::binary); <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >if</span> (!outfile) {<span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:borde R-box; " >cerr</span><<<span class= "hljs-string" style= "Color:rgb (0, 136, 0); Box-sizing:border-box; " > "Open error!"        </span><<endl; <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; " >exit</span> (<span class= "Hljs-number" style= "Color:rgB (0, 102, 102); Box-sizing:border-box; "    >1</span>); } <span class= "hljs-built_in" style= "Color:rgb (102, 0, 102); Box-sizing:border-box; "    >cin</span>>>a; Outfile.write ((<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;" >char</span>*) &a, <span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); Box-sizing:border-box; " >sizeof</span> (<span class= "Hljs-keyword" style= "Color:rgb (0, 0, 136); box-sizing:border-box;"    >int</span>));    Outfile.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>

15 Week C + + language Basics program Reading--random reading and writing of binary files and files (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.