简体   繁体   中英

How to redirect printf stream output to a .csv file?

From a sensor, have a stream of data which looks like a sequence of tuples:

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)

So far, I am printing all this data into a terminal like this:

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);

How can I redirect the above printf output tuples into a single .csv file like this:

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

You can use ">" in command line when you run your program.

Example:

./program > output.csv

Anything you print with the printf() function will be written to output.csv file.

Or you can write all data into file using fprintf() function.

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);
        }
}

You have different options to do what you ask.

Option 1 (more C++)
Use ofstream

Option 2 (C)
You can use fprintf function just like printf, giving it a file handle created with fopen() .

Option 3 (no programming)
Use output redirection to file if your console output is already exactly as you want it to be in the CSV file:

C:\\ yourprogram > output.csv

Open a file and use fprintf, passing the file handle into it.

FILE *myfile = fopen ("myfilename.csv", "w" );

And then inside your loop:

pc.printf (myfile, "\nsensor: (%f, %f, %f)\n", ax,ay,az);

And then at the end:

fclose (myfile);

Of course, this is the C-style solution to it. If you wanted to use a C++ type of interface (you tagged it as both), look into using iostreams.

If you don't mind closing output to terminal you can use

frepoen("file.csv", "w", stdout);

This will redirect your printf output into a file. You can then write your data using printf.

You can also use shell redirections like ./your_program > file.csv to redirect stdout into files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM