943,983 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17142
  • C++ RSS
Feb 23rd, 2005
0

Reading Binary files via stdin

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
Morinar is offline Offline
1 posts
since Feb 2005
Feb 23rd, 2005
0

Re: Reading Binary files via stdin

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ homework
Next Thread in C++ Forum Timeline: classes in C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC