A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FFile%2FBinary_File</id>
		<title>C++/File/Binary File - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FFile%2FBinary_File"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Binary_File&amp;action=history"/>
		<updated>2026-04-18T12:10:46Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/File/Binary_File&amp;diff=864&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Binary_File&amp;diff=864&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B/File/Binary_File&amp;diff=865&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B/File/Binary_File&amp;diff=865&amp;oldid=prev"/>
				<updated>2010-05-25T10:24:02Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Binary Files==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
int main () &lt;br /&gt;
{&lt;br /&gt;
 long start,end;&lt;br /&gt;
 ifstream myfile (&amp;quot;test.txt&amp;quot;, ios::in|ios::binary);&lt;br /&gt;
 start = myfile.tellg();&lt;br /&gt;
 myfile.seekg (0, ios::end);&lt;br /&gt;
 end = myfile.tellg();&lt;br /&gt;
 myfile.close();&lt;br /&gt;
 cout &amp;lt;&amp;lt; &amp;quot;size of &amp;quot; &amp;lt;&amp;lt; &amp;quot;test.txt&amp;quot;;&lt;br /&gt;
 cout &amp;lt;&amp;lt; &amp;quot; is &amp;quot; &amp;lt;&amp;lt; (end-start) &amp;lt;&amp;lt; &amp;quot; bytes.\n&amp;quot;;&lt;br /&gt;
 return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==binary input and output with integers==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;            &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;  &lt;br /&gt;
using namespace std;  &lt;br /&gt;
const int MAX = 100;          &lt;br /&gt;
int buff[MAX];                &lt;br /&gt;
  &lt;br /&gt;
int main(){  &lt;br /&gt;
   for(int j=0; j&amp;lt;MAX; j++)   &lt;br /&gt;
      buff[j] = j;              &lt;br /&gt;
                              &lt;br /&gt;
   ofstream os(&amp;quot;edata.dat&amp;quot;, ios::binary);  &lt;br /&gt;
                              &lt;br /&gt;
   os.write( reinterpret_cast&amp;lt;char*&amp;gt;(buff), MAX*sizeof(int) );  &lt;br /&gt;
   os.close();                &lt;br /&gt;
  &lt;br /&gt;
   for(int j=0; j&amp;lt;MAX; j++)       &lt;br /&gt;
      buff[j] = 0;  &lt;br /&gt;
                              &lt;br /&gt;
   ifstream is(&amp;quot;edata.dat&amp;quot;, ios::binary);  &lt;br /&gt;
   is.read( reinterpret_cast&amp;lt;char*&amp;gt;(buff), MAX*sizeof(int) );  &lt;br /&gt;
   for(int j=0; j&amp;lt;MAX; j++){      &lt;br /&gt;
      if( buff[j] != j ){ &lt;br /&gt;
         cerr &amp;lt;&amp;lt; &amp;quot;Data is incorrect\n&amp;quot;; return 1;&lt;br /&gt;
      }&lt;br /&gt;
   }     &lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Data is correct\n&amp;quot;;  &lt;br /&gt;
   return 0;  &lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==reading a complete binary file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
ifstream::pos_type size;&lt;br /&gt;
char * memblock;&lt;br /&gt;
int main () {&lt;br /&gt;
  ifstream file (&amp;quot;example.txt&amp;quot;, ios::in|ios::binary|ios::ate);&lt;br /&gt;
  if (file.is_open()){&lt;br /&gt;
    size = file.tellg();&lt;br /&gt;
    memblock = new char [size];&lt;br /&gt;
    file.seekg (0, ios::beg);&lt;br /&gt;
    file.read (memblock, size);&lt;br /&gt;
    file.close();&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;the complete file content is in memory&amp;quot;;&lt;br /&gt;
    delete[] memblock;&lt;br /&gt;
  }else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Unable to open file&amp;quot;;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==seeks particular person in file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
  #include &amp;lt;iostream&amp;gt;  &lt;br /&gt;
  using namespace std;  &lt;br /&gt;
  class person      &lt;br /&gt;
  {  &lt;br /&gt;
     protected:  &lt;br /&gt;
        char name[80];&lt;br /&gt;
        int age;      &lt;br /&gt;
     public:  &lt;br /&gt;
        void getData(){  &lt;br /&gt;
           cout &amp;lt;&amp;lt; &amp;quot;\n   Enter name: &amp;quot;; cin &amp;gt;&amp;gt; name;  &lt;br /&gt;
           cout &amp;lt;&amp;lt; &amp;quot;   Enter age: &amp;quot;; cin &amp;gt;&amp;gt; age;  &lt;br /&gt;
        }  &lt;br /&gt;
        void showData(void){  &lt;br /&gt;
           cout &amp;lt;&amp;lt; &amp;quot;\n   Name: &amp;quot; &amp;lt;&amp;lt; name;  &lt;br /&gt;
           cout &amp;lt;&amp;lt; &amp;quot;\n   Age: &amp;quot; &amp;lt;&amp;lt; age;  &lt;br /&gt;
        }  &lt;br /&gt;
  };  &lt;br /&gt;
  int main(){  &lt;br /&gt;
     person pers;&lt;br /&gt;
     ifstream infile;&lt;br /&gt;
     infile.open(&amp;quot;GROUP.DAT&amp;quot;, ios::in | ios::binary);&lt;br /&gt;
    &lt;br /&gt;
     infile.seekg(0, ios::end);     &lt;br /&gt;
     int endposition = infile.tellg();        &lt;br /&gt;
     int n = endposition / sizeof(person);    &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nThere are &amp;quot; &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &amp;quot; persons in file&amp;quot;;  &lt;br /&gt;
    &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nEnter person number: &amp;quot;;  &lt;br /&gt;
     cin &amp;gt;&amp;gt; n;  &lt;br /&gt;
     int position = (n-1) * sizeof(person);  &lt;br /&gt;
     infile.seekg(position);        &lt;br /&gt;
                                    &lt;br /&gt;
     infile.read( reinterpret_cast&amp;lt;char*&amp;gt;(&amp;amp;pers), sizeof(pers) );  &lt;br /&gt;
     pers.showData();               &lt;br /&gt;
     cout &amp;lt;&amp;lt; endl;  &lt;br /&gt;
     return 0;  &lt;br /&gt;
     }&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Write numbers to a binary file and read them back==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main(void){&lt;br /&gt;
   float fnum[4] = {11.22, -33.44, 55.66, 77.88};&lt;br /&gt;
   int i;&lt;br /&gt;
   ofstream out(&amp;quot;numbers.asc&amp;quot;, ios::out | ios::binary);&lt;br /&gt;
   if(!out){&lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.&amp;quot;;&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   out.write((char *) &amp;amp;fnum, sizeof(fnum));&lt;br /&gt;
   out.close();&lt;br /&gt;
   for (i=0; i&amp;lt;4; i++)&lt;br /&gt;
      fnum[i] = 0.0;&lt;br /&gt;
   ifstream in(&amp;quot;numbers.asc&amp;quot;, ios::in | ios::binary);&lt;br /&gt;
   if(!in) {&lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.&amp;quot;;&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   in.read((char *) &amp;amp;fnum, sizeof(fnum));&lt;br /&gt;
   cout &amp;lt;&amp;lt; in.gcount() &amp;lt;&amp;lt; &amp;quot; bytes read.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
   for (i=0; i&amp;lt;4; i++)&lt;br /&gt;
      cout &amp;lt;&amp;lt; fnum[i] &amp;lt;&amp;lt; &amp;quot; &amp;quot;;&lt;br /&gt;
   in.close();&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>