My main question is how do you read data from a file that is not of type char . I am writing a data file from MATLAB as follows:
x=rand(1,60000); fID=fopen('Data.txt','w'); fwrite(fID,x,'float'); fclose(fID);
Then, when I try to read it in C ++ using the following code, "num" does not change.
#include <iostream> #include <fstream> using namespace std; int main() { fstream fin("Data.txt",ios::in | ios::binary); if (!fin) { cout<<"\n Couldn't find file \n"; return 0; } float num=123; float loopSize=100e3; for(int i=0; i<loopSize; i++) { if(fin.eof()) break; fin >> num; cout<< num; } fin.close(); return 0; }
I can read and write the file in matlab in order, and I can read and write in C ++, but I can not write in matlab and read in C ++. The files I write in matlab are in the format I want, but the files in C ++ seem to write / read numbers in the text. How do you read a series of float from a file in C ++ or what am I doing wrong?
edit: The loop code is dirty because I did not want an infinite loop, and the eof flag was never set.
source share