C++ const void*转换

Pfe*_*fer 0 c++ file-io

我正在尝试将三个参数传递给我的write()函数:

write(fd, "C,1,1\r\n", 7);
Run Code Online (Sandbox Code Playgroud)

这很好用.但是我想获取一个参数并将其传递给命令部分以使其动态化:

write(fd, N, 4);
Run Code Online (Sandbox Code Playgroud)

我不熟悉c ++类型,但它一直要求一种"const void*"我已经能够将我的变量N转换成几种不同的格式,希望可以更容易转换.这是我尝试过的:

const fmx::FixPt&  outValue = dataVect.AtAsNumber(3);
const double  N = outValue.AsLong();

double  N = outValue.AsLong();
Run Code Online (Sandbox Code Playgroud)

所以double和const double(*可能几乎是一样的......我不太了解c ++)

我也可以这样做:

const fmx::FixPt&  outValue = dataVect.AtAsNumber(3);
write(fd, outValue, 4);
Run Code Online (Sandbox Code Playgroud)

但是我觉得要问每个人如何转换一个double会比尝试解释或找出类似const fmx :: FixPt&...

*我也尝试过:

write(fd, &N, 4);
Run Code Online (Sandbox Code Playgroud)

只能摆脱我的错误,但仍然无法正常工作.

那么,甚至可以转换为"const void*"的类型吗?

非常感谢你!

这是代码:

const fmx::FixPt& outValue = dataVect.AtAsNumber(3);
double  N = outValue.AsLong();

int fd;
struct termios options;
fd=open("/dev/tty.KeySerial1", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);
tcgetattr(fd,&options);
options.c_ispeed=57600;
options.c_ospeed=57600;
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~CSTOPB;
options.c_lflag &= ~ECHO;
options.c_oflag &= ~ECHO;
options.c_oflag &= ~OPOST;
options.c_cflag |= CS8;
options.c_cflag |= CRTSCTS;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] =10;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&options);


if( tempText->Assign("2"), *tempText == direction ) {
    write(fd, "C,1,2\r\n", 7);//Direction
}else{
    write(fd, "C,1,1\r\n", 7);//Direction
}

if( tempText->Assign("1"), *tempText == speed ) {
    write(fd, "C,2,1\r\n", 7);//Speed
} else if( tempText->Assign("2"), *tempText == speed ) {
    write(fd, "C,2,2\r\n", 7);//Speed
} else if( tempText->Assign("3"), *tempText == speed ) {
    write(fd, "C,2,3\r\n", 7);//Speed
} else if( tempText->Assign("4"), *tempText == speed ) {
    write(fd, "C,2,4\r\n", 7);//Speed
} else if( tempText->Assign("5"), *tempText == speed ) {
    write(fd, "C,2,5\r\n", 7);//Speed
} else if( tempText->Assign("6"), *tempText == speed ) {
    write(fd, "C,2,6\r\n", 7);//Speed
} else if( tempText->Assign("7"), *tempText == speed ) {
    write(fd, "C,2,7\r\n", 7);//Speed
} else if( tempText->Assign("8"), *tempText == speed ) {
    write(fd, "C,2,8\r\n", 7);//Speed
} else if( tempText->Assign("9"), *tempText == speed ) {
    write(fd, "C,2,9\r\n", 7);//Speed
} else if( tempText->Assign("10"), *tempText == speed ) {
    write(fd, "C,2,10\r\n", 8);//Speed
} else if( tempText->Assign("11"), *tempText == speed ) {
    write(fd, "C,2,11\r\n", 8);//Speed
} else if( tempText->Assign("12"), *tempText == speed ) {
    write(fd, "C,2,12\r\n", 8);//Speed
} else if( tempText->Assign("13"), *tempText == speed ) {
    write(fd, "C,2,13\r\n", 8);//Speed
} else if( tempText->Assign("14"), *tempText == speed ) {
    write(fd, "C,2,14\r\n", 8);//Speed
} else if( tempText->Assign("15"), *tempText == speed ) {
    write(fd, "C,2,15\r\n", 8);//Speed
} else if( tempText->Assign("16"), *tempText == speed ) {
    write(fd, "C,2,16\r\n", 8);//Speed
} else if( tempText->Assign("17"), *tempText == speed ) {
    write(fd, "C,2,17\r\n", 8);//Speed
}



if(tempText->Assign("1"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "1", 1);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("2"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "10", 2);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("3"), *tempText == length){
    write(fd, "C,3,", 4);
    write(fd, "100", 3);
    write(fd, "\r\n", 2);
} else if(tempText->Assign("4"), *tempText == length){

    write(fd, "C,3,", 4);
    write(fd, N, 4);
    write(fd, "\r\n", 2);
}


close(fd);
Run Code Online (Sandbox Code Playgroud)

错误:无法将参数'2'的'double'转换为'const void*'为'ssize_t write(int,const void*,size_t)'

jhe*_*ngs 7

write命令只需要一个数据块并写入文件描述符.所以,如果你想写你的二进制数据double,你会做类似的事情:

write(fd, &N, sizeof(double));
Run Code Online (Sandbox Code Playgroud)

请注意,这会将二进制数据而不是人类可读的ASCII数据写入文件描述符.这可能很危险,因为数据存储在本地架构的字节顺序中,但根据您的应用程序,这可能不是一个问题.

通常,在C++中,您将使用iostream运算符:

double N = 0.0;

// write data to file
ofstream out("myfile", ofstream::binary);
out << N;
out.close();

// read data back
ifstream in("myfile", ifstream::binary);
in >> N;
in.close();
Run Code Online (Sandbox Code Playgroud)

流运算符被重载以获取多种参数,包括字符串:

out << "hello world: " << 42 << endl;
Run Code Online (Sandbox Code Playgroud)