binaryfiles - C++ why does the binary file read skips to the end -


i have 178 mb fortran unformatted binary file reading c++ , storing in eigen matrix. have read file fortran , matlab confirm understand values in file. first record in file size of matrix. after each record begins 3 integers. these integers contain column , row data begins , number of numbers read. followed single precision numbers themselves.

here code:

std::ifstream infile("msfile", std::ios::in | std::ios::binary); if(!infile) {     std::cout << "mode shape .f12 file (msfile) not found\n";     exit(1); }  int r_size1; // size of each record read int r_size2; // size after reading comparison  int cols, rows; infile.read(reinterpret_cast<char *> (&r_size1), 4); infile.read(reinterpret_cast<char *> (&cols), 4); infile.read(reinterpret_cast<char *> (&rows), 4); infile.read(reinterpret_cast<char *> (&r_size2), 4);  if (r_size1 != r_size2) {     std::cout << "error reading msfile\n";     exit(1); }  matrixxf data(rows, cols); data.setzero();  int * vals = new int[3]; // vals holds i_col, i_row , i_word each record float * tempf; // pointer array of floats holds data file  // read in record, , continue through file infile.read(reinterpret_cast<char *> (&r_size1), 4);  while (!infile.eof()) {     infile.read(reinterpret_cast<char *> (vals), 12);     tempf = new float[vals[2]];     int buf_size = vals[2] * 4;     // read data file     infile.read(reinterpret_cast<char *> (tempf), buf_size);      // write float array matrix     data.col(vals[0] - 1) = map<vectorxf>(tempf, rows);      infile.read(reinterpret_cast<char *> (&r_size2), 4);      if (r_size1 != r_size2)     {         std::cout << "error reading msfile\n";         exit(1);     }      delete tempf;      // finish out reading next record size...this force eof     infile.read(reinterpret_cast<char *> (&r_size1), 4); }  delete vals;  infile.close(); 

the problem when reading float array first time, file goes end. have used infile.tellg() after each infile.read track what's happening. moves desired amount until first instance of float array. following first float array read file goes eof. expect record contain 26130 numbers. confirmed vals[2]. buf_size 104520 4 * 26130 expected. tempf not populated either.

if open binary file text file, is, if leave | std::ios::binary out of second parameter of constructor of ifstream, read function may encounter byte sequence interprets eof, part of binary data.

at least, problem have encountered when reading binary data on microsoft platforms in past, , symptoms describe (not many bytes read expected, program acts if reached eof) consistent i've observed when program read "false eof" binary file while reading in text mode.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -