gpt4 book ai didi

c++ - 如何将 printf 流输出重定向到 .csv 文件?

转载 作者:行者123 更新时间:2023-11-30 18:17:46 27 4
gpt4 key购买 nike

来自传感器的数据流看起来像元组序列:

sensor: (-0.560303, -0.627686, 0.467468)  
sensor: (-0.561829, -0.626160, 0.466125)
sensor: (-0.556091, -0.623352, 0.471497)
sensor: (-0.558411, -0.625977, 0.468811)
sensor: (-0.557312, -0.626587, 0.468262)
sensor: (-0.557800, -0.625854, 0.465820)
sensor: (-0.563599, -0.624512, 0.464722)
sensor: (-0.555847, -0.623230, 0.467163)
sensor: (-0.557861, -0.621033, 0.468811)
sensor: (-0.555420, -0.625061, 0.470520)
sensor: (-0.559082, -0.626221, 0.475891)
sensor: (-0.559814, -0.625977, 0.466309)
sensor: (-0.561768, -0.624756, 0.467163)
sensor: (-0.551941, -0.628906, 0.469055)
sensor: (-0.556946, -0.626465, 0.471313)
sensor: (-0.558533, -0.626038, 0.469421)
sensor: (-0.557922, -0.625061, 0.467285)
sensor: (-0.562622, -0.623657, 0.469971)
sensor: (-0.554443, -0.625977, 0.465759)
sensor: (-0.559265, -0.626282, 0.471619)
sensor: (-0.558716, -0.625427, 0.471375)
sensor: (-0.559143, -0.626526, 0.467468)
sensor: (-0.554749, -0.626221, 0.468079)
sensor: (-0.554626, -0.622681, 0.467285)
sensor: (-0.557983, -0.625549, 0.475464)
sensor: (-0.555603, -0.626343, 0.466980)
sensor: (-0.559570, -0.625854, 0.470398)
sensor: (-0.556946, -0.626587, 0.466858)
sensor: (-0.557373, -0.626526, 0.470093)
sensor: (-0.558716, -0.629272, 0.464905)
sensor: (-0.555725, -0.625732, 0.473877)
sensor: (-0.560608, -0.626282, 0.469238)
sensor: (-0.556335, -0.626221, 0.467590)
sensor: (-0.558777, -0.623840, 0.468994)

到目前为止,我将所有这些数据打印到这样的终端中:

if (sensor_data) {
mpu.readSensorData(sensor);
float ax = sensor[0] * 250.0 / 368.0;
float ay = sensor[1] * 250.0 / 368.0;
float az = sensor[2] * 250.0 / 368.0;
pc.printf("\nsensor: (%f, %f, %f)\n", ax,ay,az);
}*/


wait(0.5);

如何将上述 printf 输出元组重定向到单个 .csv 文件中,如下所示:

sensorX, sensorY, sensorZ
-0.560303, -0.627686, 0.467468
-0.557312, -0.626587, 0.468262
-0.557800, -0.625854, 0.465820
-0.555420, -0.625061, 0.470520
...
-0.555725, -0.625732, 0.473877

最佳答案

运行程序时,您可以在命令行中使用“">”。

示例:

./program > output.csv

使用 printf() 函数打印的任何内容都将写入 output.csv 文件。

或者您可以使用 fprintf() 函数将所有数据写入文件。

FILE* file = fopen("output.csv", "w");
if (file != NULL)
{
if (sensor_data)
{
mpu.readSensorData(sensor);
float ax = sensor[0] * 250.0 / 368.0;
float ay = sensor[1] * 250.0 / 368.0;
float az = sensor[2] * 250.0 / 368.0;
fprintf(file, "\nsensor: (%f, %f, %f)\n", ax,ay,az);
}
}

关于c++ - 如何将 printf 流输出重定向到 .csv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46730422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com