gpt4 book ai didi

c++ - 无法从 .dat 文件读取数据(从 Simulink 创建的 VS2012 C++ 项目)

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:59 27 4
gpt4 key购买 nike

编辑:谢谢,有兴趣的人的固定代码:ert_main.cpp:

#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "Rx.h" /* Model's header file */
#include "rtwtypes.h" /* MathWorks types */
#include <stdlib.h>
#include <iostream>
#include <istream>
#include <sstream>
#include <fstream>
#include <string>
//#include <ofstream>//for writing results to file
//#include <ifstream> //doesn't work
#include <vector>
#define lengthOFSignal 5000

在主要功能:

 using namespace std;
std::vector<std::string> words;
std::string word;
ifstream iFile;
string path = __FILE__; //gets source code path, include file name
path = path.substr(0,1+path.find_last_of('\\')); //removes file name
string testFileName = "LEGACY_R12_800BITS_40MHz.dat";
path+= testFileName; //adds input file to path
int signal_length=0;

std::vector<real_T> real_part_VEC, imag_part_VEC;
std::istringstream ss;
real_T real_temp, imag_temp;

iFile.open(path,ios::binary );
//iFile.open(path.c_str(), ios::binary);
if (iFile.is_open()) {
while (std::getline(iFile, word)) {
words.push_back(word);
}
iFile.close();
}

signal_length=words.size();
for(int i=0; i< signal_length;i++)
{
ss.str(words[i]);
ss >> real_temp >> imag_temp;
real_part_VEC.push_back(real_temp);
imag_part_VEC.push_back(imag_temp);
}

real_T real_part[lengthOFSignal];
real_T imag_part[lengthOFSignal];

for (int i=0; i < signal_length; i++) {

real_part[i]=real_part_VEC[i];
imag_part[i]=imag_part_VEC[i];
}

/* Initialize model */
Rx_initialize(real_part,imag_part,signal_length);

旧代码和问题:

enter image description here.dat 文件看起来像两个带有数字(间隔)的直列

real and imaginary part

我收到关于循环读取数据的错误 (strtok -> atof (Null pointer) )

修改后的 ert_main.cpp 主函数:

#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "Rx.h" /* Model's header file */
#include "rtwtypes.h" /* MathWorks types */
#include <stdlib.h>
#include "mat.h"
#define lengthOFSignal 5000
#define SizeOfLine 35

int_T main(int_T argc, const char *argv[])
{
/* Unused arguments */
(void)(argc);
(void)(argv);


int i=0;
char bFileName[] = "QPSK_SISO_802_11_packet_awgn_fr_shft.dat";
char chr[SizeOfLine*lengthOFSignal];
char *token;
real_T real_part[lengthOFSignal];
real_T image_part[lengthOFSignal];
int signal_length=0;

std::ifstream iFile(bFileName, std::ios::binary);
iFile.getline(chr,100);
for(int i=0; i<lengthOFSignal; i++) {
if (chr== NULL) break;
token= strtok(chr," ");
real_part[i]=atof(token); // real part.---problem occurs here!!!!
token= strtok(NULL," ");
image_part[i]=atof(token);// imaginary part.
iFile.getline(chr,100);
signal_length++;

}
iFile.close();

/* Initialize model */
Rx_initialize(real_part,image_part,signal_length);

/* Attach rt_OneStep to a timer or interrupt service routine with
* period 40.0 seconds (the model's base sample time) here. The
* call syntax for rt_OneStep is
*
* rt_OneStep();
*/
printf("Warning: The simulation will run forever. "
"Generated ERT main won't simulate model step behavior. "
"To change this behavior select the 'MAT-file logging' option.\n");
fflush((NULL));
while (rtmGetErrorStatus(Rx_M) == (NULL)) {
/* Perform other application tasks here */
rt_OneStep();
if(Rx_Y.EVM!=0) break;
}

/* Disable rt_OneStep() here */

/* Terminate model */
Rx_terminate();
return 0;
}

解决方案链接 (VS 2012) link to the project/solution

最佳答案

您正在尝试转换超过输入长度。那就是创建调试断言错误。作为建议,尝试以数字形式读取数据。让生活更轻松。


编辑:我要添加到之前的声明中。我使用 istringstream 将字符串解析为数字。我只是检查了这个数字,但您可以轻松扩展它。

std::string str("2.1506668e-03");
std::istringstream ss;
ss.str(str);
double x;
ss >> x;
std::cout << x << std::endl;

答案:0.0021567


新编辑:啊!这段代码好多了。但是您仍然需要消除一些非常基本的错误。

1> file.is_open 失败只能是因为找不到文件。尝试查明该文件是否在搜索路径中。最简单的方法是将文件复制到项目文件夹中。

2> 当大小不确定时,使用 vector 总是有意义的。顺便说一句,您可以使用 fseek 和 ftell 确定文件的大小,从而确定数组的大小。

3> 粗略地看一下,这条语句 std::string real_str("words[i]"); 应该改为 std::string real_str(words[i ]); 前一个将字符串 words[i] 作为输入。

4> 在 for 循环中,您正在为 signal_length 循环,但您使用的是 words[i]words[i+1]。所以在这种情况下只会读取一半的 vector 。

我只是将整行读入词 vector ,然后将其解析为实部和虚部

 std::vector<std::string> words;
std::string word;
if (fin.is_open()) {
while (std::getline(fin, word)) {
words.push_back(word);
}
fin.close();
}


// I would declare two vectors
std::vector<real_T> real_part, imag_part;
std::istringstream ss;
real_T real_temp, imag_temp;

// for loop
for(int i=0;i<words.size();i++)
{
ss.str(words[i]);
ss >> real_temp >> imag_temp;
real_part.push_back(real_temp);
imag_part.push_back(imag_temp);
}

关于c++ - 无法从 .dat 文件读取数据(从 Simulink 创建的 VS2012 C++ 项目),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com