Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7031415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:48:02+00:00 2026-05-28T00:48:02+00:00

I am trying a reasonably simple program to test binary input/output. I am basically

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:48:02+00:00Added an answer on May 28, 2026 at 12:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a storage file format for some simple data in a
I'm trying to do something that I thought ought to be reasonably simple but
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I'm trying to create an as.data.frame.by method which basically melts the N-dimensional by object
I am trying to create a small Android app with reasonably simple AR functionality
I've got a reasonably large Flex project and I'm trying to generate ASDoc documentation
Trying to setup an SSH server on Windows Server 2003. What are some good
I have been trying to get a simple example of the jquery-ui autocomplete to
Using C++/CLI and Windows Forms, I'm trying to make a simple scrollable list of
I'm trying to implement a data compression idea I've had, and since I'm imagining

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.