LaVOZs

The World’s Largest Online Community for Developers

'; Writing 1D array to different columns in a CSV C++? - LavOzs.Com

I have a function that generates a 1D array of values. I want to store that array into a CSV file. Each time I call the function I would like to store this data into a new column in the CSV. Right now my code is semi-working, it is continually adding the new data array to my csv each time I call the function but it is adding it to the bottom of the same column. I would like the data to be stored in the next column instead of being added to the bottom of the first column. Any help would be greatly appreciated.

* I have removed the section of the code that generates my standard_dev array *

#include <iostream>
#include <cmath>
#include <fstream>
#include <vector>
#include <iomanip>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "functions.h"

using namespace cv;
using namespace std;

void standard_deviation_line(Mat image, int column_count) {

    int sum = 0;
    int standard_dev_col = 0;
    int i, j; 
    const int cols = 3840;
    int rows = image.rows;
    float standard_dev[cols];
    float mean[cols];

ofstream out("SD.csv", ios::app);
    for (i = 0; i < cols; i++)
        out << standard_dev[i] << endl;

    out.close();

}

The problem is the endl in your for loop, it outputs a linefeed.

ed: try this

template<typename Iter>
std::string join(Iter p0, Iter p1) {
    std::string result{};
    if(p0 != p1) {
        result = std::to_string(*p0++);
    }
    while(p0 != p1) {
        result += ',';
        result += std::to_string(*p0++);
    }
    return result;
}

then use it without the for loop like this:

out << join(standard_dev, standard_dev + cols) << '\n';
Related
What are the differences between a pointer variable and a reference variable in C++?
Create ArrayList from array
How do I check if an array includes a value in JavaScript?
What is the difference between Python's list methods append and extend?
How to append something to an array?
The Definitive C++ Book Guide and List
What is the “-->” operator in C++?
How do I remove a particular element from an array in JavaScript?
For-each over an array in JavaScript?
Why is processing a sorted array faster than processing an unsorted array?