c++ 中的二进制 .dat 文件问题

发布于 2024-08-20 22:56:45 字数 872 浏览 5 评论 0原文

我想将具有浮点值的大型文本文件的大小缩小到二进制 .dat 文件中,因此我使用(在 c++ 中):

// the text stream
std::ifstream fin(sourceFile);
// the binary output stream
std::ofstream out(destinationFile, std::ios::binary);

float val;
while(!fin.eof())
{
    fin >> val;     
    out.write((char *)&val,sizeof(float));
}
fin.close();
out.close();

然后,我想将所有浮点值从先前创建的二进制文件读取到浮点数组中价值观。 但是当我尝试从此文件中读取时,我在最后一行代码(读取过程)中遇到异常:

// test read
std::ifstream fstream(destinationFile, std::ios::binary);

__int64 fileSize = 0;
struct __stat64 fileStat;  
if(0 == _tstat64(destinationFile, &fileStat))
{
    fileSize = fileStat.st_size;
}

//get the number of float tokens in the file
size_t tokensCount = fileSize / sizeof(float);
float* pBuff = new float[tokensCount];
fstream.read((char*)&pBuff, tokensCount * sizeof(float));

我做错了什么?

I wanted to shrink the size of a large text file with float values into a binary .dat file, so I used (in c++):

// the text stream
std::ifstream fin(sourceFile);
// the binary output stream
std::ofstream out(destinationFile, std::ios::binary);

float val;
while(!fin.eof())
{
    fin >> val;     
    out.write((char *)&val,sizeof(float));
}
fin.close();
out.close();

Then, I wanted to read all the float values from the rpeviously created binary file into a array of float values.
But when I try to read from this file I get an exception at the last line of code (the reading process):

// test read
std::ifstream fstream(destinationFile, std::ios::binary);

__int64 fileSize = 0;
struct __stat64 fileStat;  
if(0 == _tstat64(destinationFile, &fileStat))
{
    fileSize = fileStat.st_size;
}

//get the number of float tokens in the file
size_t tokensCount = fileSize / sizeof(float);
float* pBuff = new float[tokensCount];
fstream.read((char*)&pBuff, tokensCount * sizeof(float));

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

去了角落 2024-08-27 22:56:46
float* pBuff = new float[tokensCount];
fstream.read((char*)&pBuff, tokensCount * sizeof(float));

您正在读入 pBuff 变量,而不是它指向的缓冲区。你的意思是:

fstream.read((char*)pBuff, tokensCount * sizeof(float));
float* pBuff = new float[tokensCount];
fstream.read((char*)&pBuff, tokensCount * sizeof(float));

You are reading into the pBuff variable, not the buffer it points to. You mean:

fstream.read((char*)pBuff, tokensCount * sizeof(float));
时光病人 2024-08-27 22:56:46

请注意,这

while(!fin.eof())
{
    fin >> val;     
    out.write((char *)&val,sizeof(float));
}

不是读取文件的正确方法 - 它会在末尾读取垃圾值。您几乎不应该使用 eof() 函数,并且应该始终检查文件读取是否有效。正确的代码是:

while( fin >> val )
{
    out.write((char *)&val,sizeof(float));
}

Note that this:

while(!fin.eof())
{
    fin >> val;     
    out.write((char *)&val,sizeof(float));
}

is not the correct way to read a file - it will read a garbage value at the end. You should almost never use the eof() function and you should ALWAYS check that a file read worked. Correct code is:

while( fin >> val )
{
    out.write((char *)&val,sizeof(float));
}
梦纸 2024-08-27 22:56:46

马格努斯的回答是正确的,应该可以解决你的问题。我只想补充一点,如果您按照专家所说的去做并且没有使用邪恶的 C 风格转换,那么您一开始就不会有问题。如果您将最后一行更改为:

fstream.read(static_cast<char*>(&pBuff), tokensCount * sizeof(float));

那么您的程序将无法编译,并且错误消息将引导您找到解决方案。

编辑:如果 pBuff 是指向 char 以外的任何类型的指针,我的解决方案将不起作用。所以它在OP的情况下没有用。

Magnus' answer is correct and should solve your problem. I will only add that you wouldn't have had a problem in the first place if you had done as the gurus say and not used an evil C-style cast. If you change your last line to this:

fstream.read(static_cast<char*>(&pBuff), tokensCount * sizeof(float));

Then your program would have failed to compile and the error message would have led you to the solution.

EDIT: my solution does not work if pBuff is a pointer to any type other than char. So it's no use in the OP's case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文