0

I am trying a reasonably simple program to test binary input/output. I am basically writing a file with a header (string) and some data (doubles). The code is as follows:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>


int main() {
   typedef std::ostream_iterator<double> oi_t;
   typedef std::istream_iterator<double> ii_t;
   std::ofstream ofs("data.bin", std::ios::in);
   //-If file doesn't exist, create a new one now
   if(!ofs) {
      ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
   }
   else {
      ofs.close();
      ofs.open("data.bin", std::ios::out|std::ios::binary|std::ios::app);
   }

   //-Write a header consisting of length of grid subdomain and its name
   ///*
   const std::string grid = "Header";
   unsigned int olen = grid.size();
   ofs.write(reinterpret_cast<const char*>(&olen), sizeof(olen));
   ofs.write(grid.c_str(), olen);
   //*/

   //-Now write the data
   ///*
   std::vector<double> data_out;
   //std::vector<std::pair<int, int> > cell_ids;
   for(int i=0; i<100; ++i) {
      data_out.push_back(5.0*double(i) + 100.0);
   }
   ofs << std::setprecision(4);
   std::copy(data_out.begin(), data_out.end(), oi_t(ofs, " "));
   //*/
   ofs.close();

   //-Now read the binary file; first header then data
   std::ifstream ifs("data.bin", std::ios::binary);
   ///*
   unsigned int ilen;
   ifs.read(reinterpret_cast<char*>(&ilen), sizeof(ilen));
   std::string header;
   if(ilen > 0) {
      char* buf = new char[ilen];
      ifs.read(buf,ilen);
      header.append(buf,ilen);
      delete[] buf;
   }
   std::cout << "Read header: " << header << "\n";
   //*/

   ///*
   std::vector<double> data_in;
   ii_t ii(ifs);
   std::copy(ii, ii_t(), std::back_inserter(data_in));
   std::cout << "Read data size: " << data_in.size() << "\n";
   //*/
   ifs.close();

   //-Check the result
   ///*
   for(int i=0; i < data_out.size(); ++i) {
      std::cout << "Testing input/output element #" << i << " : "
                << data_out[i] << "  " << data_in[i] << "\n";
   }
   std::cout << "Element sizes: " << data_out.size() << "  " << data_in.size() <<  
            "\n";
   //*/
   return 0;
}

The problem is that when I try to write and read (and then print) both the header and the data it fails (I confirmed that it doesn't read the data then, but displays the header correctly). But when I comment out one of the write sections (header and/or data), it displays that part correctly indicating the read worked. I am sure I am not doing the read properly. Perhaps I am missing the usage of seekg somewhere.

3
  • Works for me. I get "Read header: Header\n Read data size: 100\n" then 100 Testing input/output... Commented Jan 10, 2012 at 3:20
  • Works for me too, using visual studio 2010 Commented Jan 10, 2012 at 3:34
  • That's odd. It does not work for me. The code as is, prints that reads data of size 0 and so it segfaults in the print section following that. I tested on Cygwin and Linux (RHEL5). Haven't yet tested on Windows. Commented Jan 10, 2012 at 4:02

2 Answers 2

1

The code runs fine for me. However you never check if the file is successfully opened for writing, so it could be silently failing on your system. After you open ofs you should add

if (!ofs) {
    std::cout << "Could not open file for writing" << std::endl;
    return 1;
}

And the same thing after you open ifs

if (!ifs) {
    std::cout << "Could not open file for reading" << std::endl;
    return 1;
}

Or something along those lines. Also I do not understand why you check if the file exists first since you do the same whether it exists or not.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the comment.. but this is a relatively minor issue not resolving my actual problem of reading the data in its entirety, i.e. header and doubles (I think).
Have you tried it though? The code you've posted works fine for me unless I am unable to open the file data.bin in which case it crashes.
Thanks. You are right. It works when it opens and crashes when if(ofs) fails (fail bit). I fixed this. That gives me some ideas.
0

This should work

#include <iostream>
using std::cout;
using std::cerr;
using std::cin;
using std::endl;
#include <fstream>
using std::ifstream;
#include <cstdint>

int main() {

    ifstream fin;
    fin.open("input.dat", std::ios::binary | std::ios::in);
    if (!fin) {
        cerr << "Cannot open file " << "input.dat" << endl;
        exit(1);
    }

    uint8_t input_byte;
    while (fin >> input_byte) {
        cout << "got byte " << input_byte << endl;
    }

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.