【问题标题】:In consistent "Access violation reading location" while writing to binary file using fstream在使用 fstream 写入二进制文件时一致的“访问冲突读取位置”
【发布时间】:2012-01-03 23:41:00
【问题描述】:

我正在尝试修改一段代码来对某些输入数据进行 FFT。一切顺利进行转换。当我尝试将转换写入二进制文件时,会出现我的问题:

FFT.exe 中 0x68d0ca24 (msvcr100d.dll) 处未处理的异常:0xC0000005:访问冲突读取位置 0x00161000。

最奇怪的是,并不是每次我编译和运行程序时都会发生这种情况。大约每三次它运行良好并将数据保存到文件中。

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>

#include "dft/FFT.h"
#include "ffft/FFTReal.h"

int main ()
{
    using namespace std;
    const char* dataFile = "C:/users/gad18/documents/temp/testData.dat";
    const char* transformFile = "C:/users/gad18/documents/temp/DFTtestOutput.dat";
    int length;
    off_t size;

    fstream inFile;
inFile.open( dataFile, ios_base::in | ios_base::binary );
if ( ! inFile.is_open() )
{
    cout << "Error opening " << dataFile << " for reading." << endl;
    cout << "Terminating process." << endl;
    cin.get();
    exit( EXIT_FAILURE );
}


cout << "Reading from " << dataFile << endl;
inFile.seekg( 0, ios_base::end );
size = static_cast <off_t> ( inFile.tellg() );
inFile.seekg( 0, ios_base::beg );
length = size / sizeof( float );

float* buffer = new float[ length ];
float* F = new float [length ];

inFile.read( (char*) buffer, length * sizeof( float ) );
inFile.close();

cout << size << " bytes read in." << endl;
cout << length << " float elements read into memory." << endl;

cout << "Press return key to creat DFT object..." << endl;
cin.sync();
cin.get();

cout << "Creating DFT object of length " << length << " points." << endl;

dft::FFT dft_object( length );
ffft::FFTReal<float> ffft_object( length );

int bits_out = dft_object.get_bits();
cout << "Successfully created DFT object with " << bits_out << " bit depth." << endl;

cout << "Press return key to attempt fast Fourier transform of data..." << endl;
cin.sync();
cin.get();

dft_object.compute_FFT( F, buffer );

cout << "Press return key to save transform data..." << endl;
cin.sync();
cin.get();

fstream outFile;
outFile.open( transformFile, ios_base::out | ios_base::trunc | ios_base::binary );
if ( ! outFile.is_open() )
{
    cout << "Error opening " << dataFile << " for writing." << endl;
    cout << "Terminating process." << endl;
    cin.get();
    exit( EXIT_FAILURE );
}
else
{
    outFile.write( (char*) &F, length * sizeof( float ) );
    size = outFile.tellg();
    cout << "Wrote " << size << " bytes to " << transformFile << endl;
    outFile.close();
}

delete [] buffer;
delete [] F;

cout << "Press return key to continue...";
cin.sync();
cin.get();
return 0;
} // int main()

如果这不是在问题中包含代码的正确方法,我深表歉意(此处为第一篇文章)。

异常似乎始终出现在 streambuf 的第 202 行(也许有帮助?)。

真正让我失望的是,它似乎是随机发生的,因为这是我第一次遇到这个问题,我什至不知道从哪里开始寻找错误。根据有关此异常的其他帖子,我的指针使用似乎有问题?

任何关于解决方案或如何处理运行错误的帮助将不胜感激。

谢谢你, 格雷格

【问题讨论】:

    标签: access-violation unhandled-exception


    【解决方案1】:

    我相信问题出在这里:outFile.write( (char*) &F, length * sizeof( float ) );

    我想你想要的是:

        outFile.write( (char*) F, length * sizeof( float ) );
    

    您正在从指针的地址开始转储内存,而不是从存储在指针中的地址开始。如果这两个地址在内存中有正确的关系,写就可以了,虽然文件中的一些数据会出错。但是这种行为是不可预测的,并且可能导致崩溃。

    如果我是你,我会考虑一种比表示输出数组的内存转储更强大的文件格式--

    【讨论】:

    • 谢谢,看来已经解决了。
    • 关于更强大的文件格式的其他评论。你是什​​么意思?你有什么建议?再次感谢。
    • 转储内存导致格式无法移植到具有不同架构的机器(甚至不同的编译器...)您应该格式化输出数据(即使使用标准输出生成简单的 .csv 格式会更好)
    【解决方案2】:

    由于您使用的是 Windows 机器,请尝试从 MSDN 下载应用程序验证程序。 (对不起,从我的 WP7 输入这个,所以我没有方便的链接)。启用 pageheap 和基本检查后,它可能会准确定位问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多