2013-09-26 161 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