Posted By

somada141 on 03/12/10


Tagged

data file function read binary stream


Versions (?)

Read arithmetic binary data using fstreams #2


 / Published in: C++
 

  1. unsigned short fread_unsigned_short(ifstream &input_file) { //note that the ifstream is passed by reference
  2. unsigned short num;
  3. //the cast reinterpretation is very importnant
  4. input_file.read(reinterpret_cast < char * > (&num),sizeof(unsigned short));
  5. return num;
  6. }
  7.  
  8. ifstream input_data;
  9. input_data.open("input.dat",ios::in | ios::binary); //note the modifiers that denote both input file and binary mode
  10. unsigned short num=fread_unsigned_short(input_data);
  11.  
  12. /*
  13. The above function can be generalized to any numeric datatype like integers, floats e.t.c.
  14. just replace the type 'unsigned short' with the wanted type
  15. */

Report this snippet  

You need to login to post a comment.