我正在尝试修改一段代码来获取一些输入数据的 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 行(也许有帮助?)。
真正让我失望的是它似乎是随机发生的,因为这是我第一次遇到这个问题,所以我什至不知道从哪里开始寻找错误。根据有关此异常的其他帖子,我使用指针似乎有问题?
任何有关解决方案或如何处理运行错误的帮助将不胜感激。
谢谢你,格雷格