简体   繁体   English

如何从C ++中读取文件中的数字?

[英]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. 我的主要问题是如何从不属于char数据类型的文件中读取数据。 I am writing a file of data from MATLAB as follows: 我正在从MATLAB写一个数据文件,如下所示:

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. 然后当我尝试使用以下代码在C ++中读取它时,“num”不会改变。

#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++. 我可以在matlab中读写文件,我可以在c ++中读写,但我不能用matlab编写并用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. 我在matlab中编写的文件是我想要的格式,但是c ++中的文件似乎是在文本中写入/读取数字。 How do you read a series of floats in from a file in C++, or what am I doing wrong? 你如何从C ++文件中读取一系列浮点数 ,或者我做错了什么?

edit: The loop code is messy because I didn't want an infinite loop and the eof flag was never being set. 编辑:循环代码很乱,因为我不想要一个无限循环,并且永远不会设置eof标志。

Formatted I/O using << and >> does indeed read and write numeric values as text. 使用<<>>格式化I / O确实读取和写入数值作为文本。

Presumably, Matlab is writing the floating-point values in a binary format. 据推测,Matlab正在以二进制格式编写浮点值。 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: 如果它使用与C ++相同的格式(大多数实现使用标准IEEE二进制格式),那么您可以使用未格式化的输入读取字节,并将它们重新解释为浮点值,方法如下:

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. 如果Matlab不使用兼容的格式,那么你需要找出它使用的格式,并编写一些代码来转换它。

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. 就此而言,您从Matlab编写的是一个未格式化的字节序列,根据您是否使用相同的系统,这些字节可能会也可能无法读取。 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. 您可以将这种未格式化的字节序列读入C ++程序(例如使用std::istream::read() ),但不应该考虑存储数据。

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. 使用fwrite不是最好的主意,因为这会以内部格式写出数据,这可能会也可能不容易在程序中读回。

Matlab has other ways of writing output, eg functions like fprintf. Matlab还有其他编写输出的方法,例如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++. 只需使用fprintf(fID, "%f\\n", x) ,然后你应该能够使用scanf在C / C ++中读回来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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