954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Reading Binary files via stdin

Okay, I need to read in a binary file from stdin and stick it in an array. The file contains binary representations of floating point numbers. I need to make sure that my array is sized properly based on the number of elements. It is quite easy to do with if stream as follows

ifstream file("infile.bin", ios::binary | ios::ate);

  // Since we are at the end, we can get the size
  //file.seekg(0,ifstream::end);
  long size = file.tellg();
  cout << size;
  file.seekg(0, ios::beg); //Seek back to the beginning
  float* unsorted;
  //Size input and read the file into it
  unsorted = new float[size];
  file.read((char*)unsorted, size);
  file.close();
  // Change size so it is the number of floats instead of
  // the number of characters
  size = size / 4;


However, I need this to work with a simple redirection, such as

program < infile.bin

I just can't seem to get anything that works. I appreciate your help.

Morinar
Newbie Poster
1 post since Feb 2005
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You