<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://bitpost.com/w/index.php?action=history&amp;feed=atom&amp;title=Reading_a_binary_file</id>
	<title>Reading a binary file - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://bitpost.com/w/index.php?action=history&amp;feed=atom&amp;title=Reading_a_binary_file"/>
	<link rel="alternate" type="text/html" href="https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;action=history"/>
	<updated>2026-05-09T08:53:33Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;diff=1636&amp;oldid=prev</id>
		<title>M at 15:12, 18 May 2006</title>
		<link rel="alternate" type="text/html" href="https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;diff=1636&amp;oldid=prev"/>
		<updated>2006-05-18T15:12:08Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;a href=&quot;https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;amp;diff=1636&amp;amp;oldid=1635&quot;&gt;Show changes&lt;/a&gt;</summary>
		<author><name>M</name></author>
	</entry>
	<entry>
		<id>https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;diff=1635&amp;oldid=prev</id>
		<title>M at 15:11, 18 May 2006</title>
		<link rel="alternate" type="text/html" href="https://bitpost.com/w/index.php?title=Reading_a_binary_file&amp;diff=1635&amp;oldid=prev"/>
		<updated>2006-05-18T15:11:34Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;lt;code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
// EXAMPLES&lt;br /&gt;
&lt;br /&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;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   const unsigned int Matrix_Rows = 256;&lt;br /&gt;
   const unsigned int Matrix_Cols = 256;&lt;br /&gt;
   unsigned char area_matrix[Matrix_Rows * Matrix_Cols];&lt;br /&gt;
   unsigned char pointers_matrix[Matrix_Rows][Matrix_Cols];&lt;br /&gt;
   ifstream data_file(&amp;quot;filename&amp;quot;, ios::binary);&lt;br /&gt;
&lt;br /&gt;
   // Reading an area of bytes &lt;br /&gt;
   data_file.read(area_matrix, sizeof(area_matrix));&lt;br /&gt;
&lt;br /&gt;
   // Rewinding the input file &lt;br /&gt;
   data_file.clear(); // just in case.&lt;br /&gt;
   data_file.seekg(0);&lt;br /&gt;
&lt;br /&gt;
   // Reading a two-dimensional array of bytes &lt;br /&gt;
   for (unsigned int i = 0; i &amp;lt; Matrix_Rows; ++i)&lt;br /&gt;
   {&lt;br /&gt;
     data_file.read(pointers_matrix[i],&lt;br /&gt;
                    Matrix_Cols);&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   return 0;&lt;br /&gt;
} &lt;br /&gt;
&lt;br /&gt;
#include &amp;amp;ltfstream&amp;gt;&lt;br /&gt;
#include &amp;amp;ltctime&amp;gt;&lt;br /&gt;
#include &amp;amp;ltstring&amp;gt;&lt;br /&gt;
#include &amp;amp;ltvector&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  std::ifstream in(&amp;quot;pp1.txt&amp;quot;,std::ios::binary);&lt;br /&gt;
  if (!in)&lt;br /&gt;
  {&lt;br /&gt;
     std::cout &amp;lt;&amp;lt; &amp;quot;problem with file open&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
     return 0;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  clock_t c1,c2;&lt;br /&gt;
&lt;br /&gt;
  c1 = clock();&lt;br /&gt;
&lt;br /&gt;
  in.seekg(0,std::ios::end);&lt;br /&gt;
  unsigned long length = in.tellg();&lt;br /&gt;
  in.seekg(0,std::ios::beg);&lt;br /&gt;
&lt;br /&gt;
   vector&amp;lt; char &amp;gt; vTmp(length+1,0);&lt;br /&gt;
   in.read(&amp;amp;vTmp[0],length);&lt;br /&gt;
&lt;br /&gt;
   string str;&lt;br /&gt;
   str.reserve(length);&lt;br /&gt;
   str.assign(&amp;amp;vTmp[0],&amp;amp;vTmp[0]+length); // no NULL at end&lt;br /&gt;
  &lt;br /&gt;
&lt;br /&gt;
  c2 = clock();&lt;br /&gt;
&lt;br /&gt;
  std::cout &amp;lt;&amp;lt; static_cast&amp;lt; double &amp;gt;(c2-c1)/static_cast&amp;lt; double &amp;gt;(CLOCKS_PER_SEC) &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
directly into std::string&lt;br /&gt;
&lt;br /&gt;
#include &amp;amp;ltiostream&amp;gt;&lt;br /&gt;
#include &amp;amp;ltfstream&amp;gt;&lt;br /&gt;
#include &amp;amp;ltctime&amp;gt;&lt;br /&gt;
#include &amp;amp;ltstring&amp;gt;&lt;br /&gt;
&lt;br /&gt;
using namespace std;&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
std::ifstream in(&amp;quot;pp1.txt&amp;quot;,std::ios::binary);&lt;br /&gt;
if (!in)&lt;br /&gt;
{&lt;br /&gt;
std::cout &amp;lt;&amp;lt; &amp;quot;problem with file open&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
clock_t c1,c2;&lt;br /&gt;
&lt;br /&gt;
c1 = clock();&lt;br /&gt;
&lt;br /&gt;
in.seekg(0,std::ios::end);&lt;br /&gt;
unsigned long length = in.tellg();&lt;br /&gt;
in.seekg(0,std::ios::beg);&lt;br /&gt;
&lt;br /&gt;
string str(length,0);&lt;br /&gt;
std::copy( std::istreambuf_iterator&amp;lt; char &amp;gt;(in) ,&lt;br /&gt;
std::istreambuf_iterator&amp;lt; char &amp;gt;() ,&lt;br /&gt;
str.begin() );&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
c2 = clock();&lt;br /&gt;
&lt;br /&gt;
std::cout &amp;lt;&amp;lt; static_cast&amp;lt; double &amp;gt;(c2-c1)/static_cast&amp;lt; double &amp;gt;(CLOCKS_PER_SEC) &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
&lt;br /&gt;
return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;/div&gt;</summary>
		<author><name>M</name></author>
	</entry>
</feed>