Reading a binary file: Difference between revisions

From Bitpost wiki
No edit summary
 
No edit summary
 
Line 1: Line 1:
<code>
<code>


// EXAMPLES
  // EXAMPLES
  /*
  #include <iostream>
  #include <fstream>
  using namespace std;


  int main(void)
  {
      const unsigned int Matrix_Rows = 256;
      const unsigned int Matrix_Cols = 256;
      unsigned char area_matrix[Matrix_Rows * Matrix_Cols];
      unsigned char pointers_matrix[Matrix_Rows][Matrix_Cols];
      ifstream data_file("filename", ios::binary);
      // Reading an area of bytes
      data_file.read(area_matrix, sizeof(area_matrix));
      // Rewinding the input file
      data_file.clear(); // just in case.
      data_file.seekg(0);


#include <iostream>
      // Reading a two-dimensional array of bytes
#include <fstream>
      for (unsigned int i = 0; i < Matrix_Rows; ++i)
using namespace std;
      {
      data_file.read(pointers_matrix[i],
                    Matrix_Cols);
      }


int main(void)
      return 0;
{
   }
  const unsigned int Matrix_Rows = 256;
   const unsigned int Matrix_Cols = 256;
  unsigned char area_matrix[Matrix_Rows * Matrix_Cols];
  unsigned char pointers_matrix[Matrix_Rows][Matrix_Cols];
  ifstream data_file("filename", ios::binary);


   // Reading an area of bytes
   #include &ltfstream>
   data_file.read(area_matrix, sizeof(area_matrix));
   #include &ltctime>
  #include &ltstring>
  #include &ltvector>


   // Rewinding the input file
   using namespace std;
  data_file.clear(); // just in case.
  data_file.seekg(0);


   // Reading a two-dimensional array of bytes
   int main()
   for (unsigned int i = 0; i < Matrix_Rows; ++i)
   {
  std::ifstream in("pp1.txt",std::ios::binary);
  if (!in)
   {
   {
    data_file.read(pointers_matrix[i],
      std::cout << "problem with file open" << std::endl;
                    Matrix_Cols);
      return 0;
   }
   }


   return 0;
   clock_t c1,c2;
}


#include &ltfstream>
  c1 = clock();
#include &ltctime>
#include &ltstring>
#include &ltvector>


using namespace std;
  in.seekg(0,std::ios::end);
  unsigned long length = in.tellg();
  in.seekg(0,std::ios::beg);


int main()
      vector< char > vTmp(length+1,0);
{
      in.read(&vTmp[0],length);
  std::ifstream in("pp1.txt",std::ios::binary);
  if (!in)
  {
    std::cout << "problem with file open" << std::endl;
    return 0;
  }


  clock_t c1,c2;
      string str;
      str.reserve(length);
      str.assign(&vTmp[0],&vTmp[0]+length); // no NULL at end
   


  c1 = clock();
  c2 = clock();


  in.seekg(0,std::ios::end);
  std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;
  unsigned long length = in.tellg();
  in.seekg(0,std::ios::beg);


   vector< char > vTmp(length+1,0);
   return 0;
   in.read(&vTmp[0],length);
   }


   string str;
   directly into std::string
  str.reserve(length);
  str.assign(&vTmp[0],&vTmp[0]+length); // no NULL at end
 


  c2 = clock();
  #include &ltiostream>
  #include &ltfstream>
  #include &ltctime>
  #include &ltstring>


  std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;
  using namespace std;


  return 0;
  int main()
}
  {
  std::ifstream in("pp1.txt",std::ios::binary);
  if (!in)
  {
  std::cout << "problem with file open" << std::endl;
  return 0;
  }


directly into std::string
  clock_t c1,c2;


#include &ltiostream>
  c1 = clock();
#include &ltfstream>
#include &ltctime>
#include &ltstring>


using namespace std;
  in.seekg(0,std::ios::end);
  unsigned long length = in.tellg();
  in.seekg(0,std::ios::beg);


int main()
  string str(length,0);
{
  std::copy( std::istreambuf_iterator< char >(in) ,
std::ifstream in("pp1.txt",std::ios::binary);
  std::istreambuf_iterator< char >() ,
if (!in)
  str.begin() );
{
std::cout << "problem with file open" << std::endl;
return 0;
}


clock_t c1,c2;


c1 = clock();
  c2 = clock();


in.seekg(0,std::ios::end);
  std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;
unsigned long length = in.tellg();
in.seekg(0,std::ios::beg);


string str(length,0);
  return 0;
std::copy( std::istreambuf_iterator< char >(in) ,
  }
std::istreambuf_iterator< char >() ,
  */
str.begin() );
 
 
c2 = clock();
 
std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;


return 0;
}


</code>
</code>

Latest revision as of 15:12, 18 May 2006

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


  c2 = clock();
  std::cout << static_cast< double >(c2-c1)/static_cast< double >(CLOCKS_PER_SEC) << std::endl;
  return 0;
  }
  */