【问题标题】:Running into an issue where I need to convert double array into char遇到我需要将双数组转换为字符的问题
【发布时间】:2015-03-08 06:26:57
【问题描述】:

所以我的问题是我不知道如何将文本文件(其中包含用 Venus 2 编写的 1000 个逐行命令)正确输入到我试图重复创建的 while 循环中向电机发送位置命令。有一个文本文件,其中包含用 Venus 2 命令编写的坐标(鲜为人知的语言;使用起来很烦人)。我不是 c++ 计算机编程的狂热爱好者,但我知道大多数基本和中级 c++ 的理想。如果您有任何建议或cmets,请告诉我您的想法!

另外,这是文件的外观的 sn-p

0.0229 1 纳米 0.0317 2 纳米
0.0276 1 纳米 0.0311 2 纳米
0.0323 1 纳米 0.0302 2 纳米
0.0347 1 纳米 0.0310 2 纳米
0.0364 1 纳米 0.0310 2 纳米
0.0422 1 纳米 0.0343 2 纳米
0.0466 1 纳米 0.0324 2 纳米

位置、电机 ID、运动类型、位置、电机 ID、运动类型
(NI Pollux II 电机 x2)

    //text file is opened

    int i = 0;
    double c1[1000];
    char line;

    cout << "Press any key to begin jitter.\n";
    cin.get();
    cout << "Running the jitter data...\n";

    /* here there should be a conversion from double to char */

    while (!jitterFile.eof()) {
        jitterFile >> c1[i];
        /* or here there should be a conversion */

        if (serial.Open(18, 19200)){
            static char szMessage[] = /* here is where the converted data should go */;
            int nBytesSent = serial.SendData(szMessage, strlen(szMessage));
            assert(nBytesSent == strlen(szMessage));
        }
        else{
            cout << "error occurred with runJitter()...\n";
        }

        cout << c1[i] << endl;
        ++i;
    }
    //Sleep(1000);

    jitterFile.close();

    return 0;
}

【问题讨论】:

标签: c++ file char double typeconverter


【解决方案1】:

首先,考虑是否需要将其转换为双精度。我看到您正在构建要在串行端口上发送的字符串消息。

这个例子应该告诉你如何做你想做的事。如果您需要这些位置供以后使用,那么我建议不要使用双数组。相反,阅读每一行,将其分解为字符串组件,然后构建您的字符串消息以发送到串行端口。如果您确实需要将位置作为双打,请将它们推到双端队列的后面以备后用。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <deque>
#include <cstdlib>

using namespace std;

size_t sendToSerial(const char* msg, size_t len) {
    return len;
}

int main(int argc, const char** argv) {
    ifstream jitterFile("jitter.txt");

    int i = 0;
    std::deque< double > c1Deque;
    //double c1[1000];
    //char line;

    cout << "Press any key to begin jitter.\n";
    cin.get();
    cout << "Running the jitter data...\n";

    /* here there should be a conversion from double to char */

    string line;
    string pos1;
    string motor1;
    string motion1;
    string pos2;
    string motor2;
    string motion2;


    while (getline(jitterFile, line)) {
        std::istringstream sstrm(line);
        sstrm >> pos1 >> motor1 >> motion1 >> pos2 >> motor2 >> motion2;

        /* or here there should be a conversion */
        c1Deque.push_back(atof(pos1.c_str()));

        if (true/*serial.Open(18, 19200)*/){
            std::string message = pos1;
            message += ",";
            message += pos2;
            int nBytesSent = sendToSerial/*serial.SendData*/(message.c_str(), message.length());
            //assert(nBytesSent == strlen(szMessage));
        }
        else{
            cout << "error occurred with runJitter()...\n";
        }

        cout << pos1 << endl;
        ++i;
    }
    //Sleep(1000);

    std::cout << "There are " << c1Deque.size() << " entries in the deque" << std::endl;
    jitterFile.close();

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-28
    • 1970-01-01
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多