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_Tutorial%2FFile_Stream%2Fbinary_file</id>
		<title>C++ Tutorial/File Stream/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_Tutorial%2FFile_Stream%2Fbinary_file"/>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/binary_file&amp;action=history"/>
		<updated>2026-04-18T07:41:18Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/File_Stream/binary_file&amp;diff=2948&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_Tutorial/File_Stream/binary_file&amp;diff=2948&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</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_Tutorial/File_Stream/binary_file&amp;diff=2949&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_Tutorial/File_Stream/binary_file&amp;diff=2949&amp;oldid=prev"/>
				<updated>2010-05-25T10:31:11Z</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;==Append to a 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;#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()&lt;br /&gt;
{&lt;br /&gt;
  ofstream outFile(&amp;quot;FileWrite.out&amp;quot;);&lt;br /&gt;
  if (outFile.fail()) {&lt;br /&gt;
    cerr &amp;lt;&amp;lt; &amp;quot;Unable to open file for writing.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  outFile &amp;lt;&amp;lt; &amp;quot;Hello!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  outFile.close();&lt;br /&gt;
  ofstream appendFile(&amp;quot;FileWrite.out&amp;quot;, ios_base::app);&lt;br /&gt;
  if (appendFile.fail()) {&lt;br /&gt;
    cerr &amp;lt;&amp;lt; &amp;quot;Unable to open file for writing.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  appendFile &amp;lt;&amp;lt; &amp;quot;Append!&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
  appendFile.close();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Creating a randomly accessed file==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cerr;&lt;br /&gt;
using std::endl;&lt;br /&gt;
using std::ios;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
using std::ofstream;&lt;br /&gt;
#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;
using std::exit;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using std::string;&lt;br /&gt;
class Account&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
    Account( int accountNumberValue, string lastNameValue, string firstNameValue, double balanceValue )&lt;br /&gt;
    {&lt;br /&gt;
       setAccountNumber( accountNumberValue );&lt;br /&gt;
       setLastName( lastNameValue );&lt;br /&gt;
       setFirstName( firstNameValue );&lt;br /&gt;
       setBalance( balanceValue );&lt;br /&gt;
    }&lt;br /&gt;
    int getAccountNumber() const&lt;br /&gt;
    {&lt;br /&gt;
       return accountNumber;&lt;br /&gt;
    }&lt;br /&gt;
    void setAccountNumber( int accountNumberValue )&lt;br /&gt;
    {&lt;br /&gt;
       accountNumber = accountNumberValue; // should validate&lt;br /&gt;
    }&lt;br /&gt;
    string getLastName() const&lt;br /&gt;
    {&lt;br /&gt;
       return lastName;&lt;br /&gt;
    }&lt;br /&gt;
    void setLastName( string lastNameString )&lt;br /&gt;
    {&lt;br /&gt;
       const char *lastNameValue = lastNameString.data();&lt;br /&gt;
       strncpy( lastName, lastNameValue, 5 );&lt;br /&gt;
       lastName[ 5 ] = &amp;quot;\0&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    string getFirstName() const&lt;br /&gt;
    {&lt;br /&gt;
       return firstName;&lt;br /&gt;
    }&lt;br /&gt;
    void setFirstName( string firstNameString )&lt;br /&gt;
    {&lt;br /&gt;
       const char *firstNameValue = firstNameString.data();&lt;br /&gt;
       strncpy( firstName, firstNameValue, 5 );&lt;br /&gt;
       firstName[ 5 ] = &amp;quot;\0&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    double getBalance() const&lt;br /&gt;
    {&lt;br /&gt;
       return balance;&lt;br /&gt;
    }&lt;br /&gt;
    void setBalance( double balanceValue )&lt;br /&gt;
    {&lt;br /&gt;
       balance = balanceValue;&lt;br /&gt;
    }&lt;br /&gt;
private:&lt;br /&gt;
   int accountNumber;&lt;br /&gt;
   char lastName[ 15 ];&lt;br /&gt;
   char firstName[ 10 ];&lt;br /&gt;
   double balance;&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   ofstream outCredit( &amp;quot;credit.dat&amp;quot;, ios::binary );&lt;br /&gt;
   if ( !outCredit )&lt;br /&gt;
   {&lt;br /&gt;
      cerr &amp;lt;&amp;lt; &amp;quot;File could not be opened.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
      exit( 1 );&lt;br /&gt;
   }&lt;br /&gt;
   Account blankClient(1,&amp;quot;AAAAA&amp;quot;,&amp;quot;BBBBB&amp;quot;,1.2);&lt;br /&gt;
   // output 100 blank records to file&lt;br /&gt;
   for ( int i = 0; i &amp;lt; 100; i++ )&lt;br /&gt;
      outCredit.write( reinterpret_cast&amp;lt; const char * &amp;gt;( &amp;amp;blankClient ),&lt;br /&gt;
         sizeof( Account ) );&lt;br /&gt;
   return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Open a 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;#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(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  char ch;&lt;br /&gt;
  ifstream in(&amp;quot;test.txt&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;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  while(in) { // in will be false when eof is reached&lt;br /&gt;
    in.get(ch);&lt;br /&gt;
    if(in) cout &amp;lt;&amp;lt; ch;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;R 9.9&lt;br /&gt;
T 9.9&lt;br /&gt;
M 4.8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Open a binary file and read==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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(int argc, char *argv[]){&lt;br /&gt;
   char ch;&lt;br /&gt;
   ifstream in(&amp;quot;test.txt&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;
      return 1;&lt;br /&gt;
   }&lt;br /&gt;
   while(in){&lt;br /&gt;
      in.get(ch);&lt;br /&gt;
      cout &amp;lt;&amp;lt; ch;&lt;br /&gt;
   }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Output a binary file in hexadecimal==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#include &amp;lt;iomanip&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
int main(int argc, char *argv[]){&lt;br /&gt;
   ifstream in(&amp;quot;text.txt&amp;quot;, ios::in | ios::binary);&lt;br /&gt;
   if(!in){&lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Cannot open input file.&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
      exit (1);&lt;br /&gt;
   }&lt;br /&gt;
   int i, j;&lt;br /&gt;
   int count = 0;&lt;br /&gt;
   char c[16];&lt;br /&gt;
   cout.setf(ios::uppercase);&lt;br /&gt;
   while(!in.eof()){&lt;br /&gt;
      for(i=0; i&amp;lt;16 &amp;amp;&amp;amp; !in.eof(); i++)&lt;br /&gt;
         in.get(c[i]);&lt;br /&gt;
      if(i&amp;lt;16)&lt;br /&gt;
         i--;&lt;br /&gt;
      for(j=0; j &amp;lt; i; j++)&lt;br /&gt;
         cout &amp;lt;&amp;lt; setw(3) &amp;lt;&amp;lt; hex &amp;lt;&amp;lt; (int) c[j];&lt;br /&gt;
      for(; j &amp;lt; 16; j++)&lt;br /&gt;
         cout &amp;lt;&amp;lt; &amp;quot;  &amp;quot;;&lt;br /&gt;
   &lt;br /&gt;
      for(j=0; j &amp;lt; i; j++)&lt;br /&gt;
         if(isprint(c[j]))&lt;br /&gt;
            cout &amp;lt;&amp;lt; c[j];&lt;br /&gt;
         else&lt;br /&gt;
            cout &amp;lt;&amp;lt; &amp;quot;.&amp;quot;;&lt;br /&gt;
      cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
      count ++;&lt;br /&gt;
      if(count==16){&lt;br /&gt;
         count = 0;&lt;br /&gt;
         cout &amp;lt;&amp;lt; &amp;quot;Press ENTER to continue: &amp;quot;;&lt;br /&gt;
         cin.get();&lt;br /&gt;
         cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
       }&lt;br /&gt;
    }&lt;br /&gt;
    in.close();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Reading 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;#include &amp;lt;iostream.h&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream.h&amp;gt;&lt;br /&gt;
int main () {&lt;br /&gt;
  char * buffer;&lt;br /&gt;
  long size;&lt;br /&gt;
  ifstream file (&amp;quot;example.dat&amp;quot;, ios::in|ios::binary|ios::ate);&lt;br /&gt;
  size = file.tellg();&lt;br /&gt;
  file.seekg (0, ios::beg);&lt;br /&gt;
  buffer = new char [size];&lt;br /&gt;
  file.read (buffer, size);&lt;br /&gt;
  file.close();&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;the complete file is in a buffer&amp;quot;;&lt;br /&gt;
  delete[] buffer;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;the complete file is in a buffer&amp;quot;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Unformatted and Binary I/O==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  char ch;&lt;br /&gt;
   &lt;br /&gt;
  if(argc!=2) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Usage: PR &amp;lt;filename&amp;gt;\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  ifstream in(argv[1], 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;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  while(in) { // in will be false when eof is reached&lt;br /&gt;
    in.get(ch);&lt;br /&gt;
    if(in) cout &amp;lt;&amp;lt; ch;&lt;br /&gt;
  }&lt;br /&gt;
   &lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use read() to input blocks of binary data.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;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;
struct inventory {&lt;br /&gt;
  char item[20];&lt;br /&gt;
  int quantity;&lt;br /&gt;
  double cost;&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  ifstream fin(&amp;quot;InvDat.dat&amp;quot;, ios::in | ios::binary);&lt;br /&gt;
  if(!fin) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  inventory inv[3];&lt;br /&gt;
  for(int i=0; i&amp;lt;3; i++)&lt;br /&gt;
    fin.read((char *) &amp;amp;inv[i], sizeof(inventory));&lt;br /&gt;
  fin.close();&lt;br /&gt;
  if(!fin.good()) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;A file error occurred.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  for(int i=0; i &amp;lt; 3; i++) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; inv[i].item &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot; Quantity on hand: &amp;quot; &amp;lt;&amp;lt; inv[i].quantity;&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;\n Cost: &amp;quot; &amp;lt;&amp;lt; inv[i].cost &amp;lt;&amp;lt; &amp;quot;\n\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Use write() to output a block of binary data.==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
struct inventory {&lt;br /&gt;
  char item[20];&lt;br /&gt;
  int quantity;&lt;br /&gt;
  double cost;&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  ofstream fout(&amp;quot;InvDat.dat&amp;quot;, ios::out | ios::binary);&lt;br /&gt;
  if(!fout) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  inventory inv[3];&lt;br /&gt;
  strcpy(inv[0].item,&amp;quot;A&amp;quot;);&lt;br /&gt;
  inv[0].quantity = 3;&lt;br /&gt;
  inv[0].cost = 9.99;&lt;br /&gt;
  strcpy(inv[1].item, &amp;quot;B&amp;quot;);&lt;br /&gt;
  inv[1].quantity = 12;&lt;br /&gt;
  inv[1].cost = 7.85;&lt;br /&gt;
  strcpy(inv[2].item, &amp;quot;C&amp;quot;);&lt;br /&gt;
  inv[2].quantity = 19;&lt;br /&gt;
  inv[2].cost = 2.75;&lt;br /&gt;
  for(int i=0; i&amp;lt;3; i++)&lt;br /&gt;
    fout.write((const char *) &amp;amp;inv[i], sizeof(inventory));&lt;br /&gt;
  fout.close();&lt;br /&gt;
  if(!fout.good()) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;A file error occurred.&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Write Unformatted Binary Data to a File==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
struct inventory {&lt;br /&gt;
  char item[20];&lt;br /&gt;
  int quantity;&lt;br /&gt;
  double cost;&lt;br /&gt;
};&lt;br /&gt;
int main(int argc, char *argv[])&lt;br /&gt;
{&lt;br /&gt;
  inventory entry;&lt;br /&gt;
  long record_num = 2;&lt;br /&gt;
  ifstream fInvDB(&amp;quot;InvDat.dat&amp;quot;, ios::in | ios::binary);&lt;br /&gt;
  if(!fInvDB) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Cannot open file.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
  fInvDB.seekg(sizeof(inventory) * record_num, ios::beg);&lt;br /&gt;
  fInvDB.read((char *) &amp;amp;entry, sizeof(inventory));&lt;br /&gt;
  fInvDB.close();&lt;br /&gt;
  if(!fInvDB.good()) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;A file error occurred.\n&amp;quot;;&lt;br /&gt;
    return 1;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>