2013-09-26 227 views
1

编辑:谢谢您,对于那些有兴趣谁的固定码: ert_main.cpp:无法读取.dat文件(VS2012 C来自Simulink中创建++项目)数据

#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 

在主FUNC:

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文件看起来像两个连胜columnes用数字(间隔)

real and imaginary part

我得到关于循环的readed数据错误(strtok的 - > ATOF(空指针))

编辑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

+0

你必须通过代码调试和检查解析循环? –

+0

@shunyo C代码是从MATLAB/Simulink模型自动生成的,请检查标题。不建议手动编辑代码。修复模型并重新生成他的代码是解决这个问题的正确方法。 – am304

+0

@ am304我的错。删除评论。 – shunyo

回答

1

您试图转换超过输入长度。这是创建调试断言错误。作为建议,尝试以数字读取数据。让生活变得更轻松。


编辑:我添加到前面的语句。我用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>当尺寸未确定时,使用矢量总是有意义的。顺便说一句,你可以使用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]。所以在这种情况下只能读出一半的矢量。

我只想读行整体进入词汇向量,然后将其解析为实部和虚部

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); 
} 
+0

我可以进入一个状态,我在每个循环迭代中使用上述库函数存储在变量中的相关字符串? –

+0

I尝试使用向量为了做到上述(我有一个关于为真实和图像部分数组分配空间的错误 - 数组的大小仅在运行时才知道,因此我切换到向量。但整个转换的代码使用real_T的数组。 = /。我可以使用矢量大小以某种方式从矢量创建一个数组,如http://stackoverflow.com/questions/7488039/expression-must-have-a-constant-value-error-in-c,或者我应该分配预先足够的记忆? –

+0

对您的代码进行了一些更改。这应该工作。 – shunyo