遇到我需要将双数组转换为字符的问题
Running into an issue where I need to convert double array into char
所以我的问题是我不知道如何让文本文件(里面装满了 1000 个用 Venus 2 编写的逐行命令)正确输入到我试图创建的 while 循环中,用于重复向电机发送位置命令。有一个文本文件,其中包含用Venus 2命令编写的坐标(鲜为人知的语言;使用起来非常烦人)。我不是 c++ 计算机编程的爱好者,但我确实知道大多数基本和中级 c++ 的理想。 如果您有任何建议或意见,请告诉我您的想法!!
另外,这是文件外观的片段
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, 运动类型
(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;
}
首先,考虑是否需要将其转换为双精度。 我看到您正在构建要在串行端口上发送的字符串消息。
此示例应向您展示如何执行要执行的操作。 如果您需要这些位置供以后使用,那么我建议不要使用双精度数组。 相反,读取每一行,将其分解为字符串组件,然后构建字符串消息以发送到串行端口。 如果您确实需要双倍的位置,请将它们推到双倍<双倍>的后面,以便以后使用。双倍>
#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;
}
相关文章:
- HEX值到wchar_t字符(UTF-8)的转换
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 转换特殊字符(UTF-8)
- 将转换字符键入 int 以用作向量C++的索引
- 将字符随机转换为大写的函数
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 转换字符时Arduino DUE 和 Arduino UNO 之间的区别
- 如何在C++中正确重载运算符转换字符*?
- 如何转换数据结构以在 c++ 中转换字符*
- 不兼容的转换字符
- 将转换字符数组[16]键入为int 128位
- 在c++中转换字符编码
- 使用递归转换字符
- 如何在Objective-C/C/C++中转换字符和字节的位置
- C++到Pep/8汇编语言的转换——字符数组
- 转换字符>十六进制