Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading floating numbers from bin file continuosly and outputting in console window

I am making a code in visual c++ to read ( and see in console )the floating numbers from a bin file, The binary file contains around 2.5 million floating point numbers.

int main(){

   char* holder;

  ifstream fin;

  fin.open("male_16_down.bin",ios::binary|ios::in);

  if(!fin){

   cout<<" Error, Couldnt find the file"<<endl;

  }

  fin.seekg(0,ios::end);

  long int file_size_bin=fin.tellg();

  long int file_size=fin.tellg();

  fin.seekg(0,ios::beg);

  file_size=file_size/sizeof(float);

  holder=new char[file_size_bin];


  fin.read(holder,file_size*sizeof(float));

  float data=(float)atof(holder);

  cout<<data<<endl;



delete[] holder; 

i know that 4 bytes for a float and 1 byte for char, this code outputs only one number that is 0,i believe atof() converts only the first byte to the number, but i want the whole number to be seen and how can i see all the numbers in the binary file, any help will be highly appreciated.

like image 634
sarath Avatar asked Oct 19 '25 13:10

sarath


1 Answers

#include <fstream>
#include <iostream>

int main() {
    float f;
    std::ifstream fin("male_16_down.bin", std::ios::binary);
    while (fin.read(reinterpret_cast<char*>(&f), sizeof(float)))
        std::cout << f << '\n';
    return 0;
}
like image 52
Pete Becker Avatar answered Oct 21 '25 03:10

Pete Becker