我在用 C++ 读取文本文件时遇到问题,我无法弄清楚哪里出了问题。我需要从文件中读取一些复数并使用 matlab 函数进行一些计算。所以在那之前我编译了 matlab 库并将它们包含到我的项目中。我测试了它们并且它工作正常。但是当我添加几行代码来从文件中读取数据时,我得到了一些错误: 这是我的代码:
#include "spectrum.h"
#include <iostream>
#include <fstream>
#include <complex>
using namespace std;
int main(){
mclInitializeApplication(NULL,0);
spectrumInitialize();
cout<<"Initialization success"<<endl;
double input[4] = {10,20,30,40};
mxArray *x_ptr, *x_ptr1;
mxArray *y_ptr=NULL;
mxArray *y_ptr1=NULL;
double *y;
double *y1;
complex<double> com[256];
ifstream myfile("dep.txt");
double a = NULL;
for(int i=0; i<256;i++)
{
myfile >> a;
com[i].real(a);
myfile >> a;
com[i].imag(a);
}
myfile.close();
x_ptr1 = mxCreateDoubleScalar(256);
x_ptr = mxCreateDoubleMatrix(1,256,mxCOMPLEX);
memcpy((void *)mxGetPr(x_ptr), (void *) com,256*sizeof(complex<double>));
mlfSpectrum_slice(1,&y_ptr,x_ptr,x_ptr1);
y = (double*)mxGetPr(y_ptr);
cout<<"Data: "<<input<<endl;
cout<<"Result: "<<*y<<endl;
mxDestroyArray(x_ptr);
mxDestroyArray(y_ptr);
spectrumTerminate();
mclTerminateApplication();
return 0;
}
我也尝试过使用 fstream 和 ifstream。有人知道我做错了什么吗? 顺便说一句,我使用的是 Windows 7 32 位、Visual Studio 2012 和 Matlab R2012b。
最佳答案
你有一个链接问题。链接器找不到函数 CrtDbgReport。这是 Microsoft 的调试功能。这一切都是关于使用 OutputDebugString API 编写调试消息。 Microsoft lib 是 kernel32.lib - 所以你有链接吗?具体来说,它正在寻找 Unicode 版本,即末尾的 W:CrtDbgReportW。所以你一定在某处设置了 Unicode 构建。即#define UNICODE。
您的库是否可能未使用 Unicode。
另一种可能性是您正在链接调试 matlab 库,但您正在构建程序的发布版本。反之亦然。
关于c++ - 读取文本文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579599/