简体   繁体   中英

How can I read numbers from a file in C++?

My main question is about how you read data from a file that is not of the char data type. I am writing a file of data 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" doesn't 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 file in matlab fine, and I can read and write in c++, but I can't 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 be writing/reading the numbers out at text. How do you read a series of floats in from a file in C++, or what am I doing wrong?

edit: The loop code is messy because I didn't want an infinite loop and the eof flag was never being set.

Formatted I/O using << and >> does indeed read and write numeric values as text.

Presumably, Matlab is writing the floating-point values in a binary format. If it uses the same format as C++ (most implementations of which use the standard IEEE binary format), then you could read the bytes using unformatted input, and reinterpret them as a floating-point value, along the lines of:

float f;  // Might need to be "double", depending on format
fin.read(reinterpret_cast<char*>(&f), sizeof f);

If Matlab does not use a compatible format, then you'll need to find out what format it does use and write some code to convert it.

You need to read and write the same format. For that matter, what you have written from Matlab is an unformatted sequence of bytes which may or may not be able read depending on whether you use the same system. You can probably read this unformatted sequence of bytes into a C++ program (eg using std::istream::read() ) but you shouldn't consider the data to be stored.

To actually store data, you need to be aware of the format the data has. The format can be binary or text but you should be clear about what the bytes mean, in which order they appear, how many there are or how to detect the end if a value, etc.

Using fwrite is not the best idea, because this will write out the data in an internal format, which might or might not be easy to read back in your program.

Matlab has other ways of writing output, eg functions like fprintf. Better write out your data this way, then it should be obvious how to read it back into another application.

Just use fprintf(fID, "%f\\n", x) , and then you should be able to use scanf to read this back in C/C++.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM