Reading Binary files via stdin

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 1
Reputation: Morinar is an unknown quantity at this point 
Solved Threads: 0
Morinar Morinar is offline Offline
Unverified User

Reading Binary files via stdin

 
0
  #1
Feb 23rd, 2005
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

  1. ifstream file("infile.bin", ios::binary | ios::ate);
  2.  
  3. // Since we are at the end, we can get the size
  4. //file.seekg(0,ifstream::end);
  5. long size = file.tellg();
  6. cout << size;
  7. file.seekg(0, ios::beg); //Seek back to the beginning
  8. float* unsorted;
  9. //Size input and read the file into it
  10. unsorted = new float[size];
  11. file.read((char*)unsorted, size);
  12. file.close();
  13. // Change size so it is the number of floats instead of
  14. // the number of characters
  15. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Reading Binary files via stdin

 
0
  #2
Feb 23rd, 2005
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC