Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将一维数组写入CSV C++;?_C++_Arrays_Csv_Append_Ofstream - Fatal编程技术网

C++ 将一维数组写入CSV C++;?

C++ 将一维数组写入CSV C++;?,c++,arrays,csv,append,ofstream,C++,Arrays,Csv,Append,Ofstream,我有一个函数,可以生成一个1D值数组。我想将该数组存储到CSV文件中。每次调用函数时,我都希望将此数据存储到CSV中的新列中。现在,我的代码处于半工作状态,每次调用该函数时,它都会不断地将新的数据数组添加到csv中,但它会将其添加到同一列的底部。我希望数据存储在下一列中,而不是添加到第一列的底部。任何帮助都将不胜感激 *我已经删除了生成标准\u dev数组的代码部分* #include <iostream> #include <cmath> #include <fs

我有一个函数,可以生成一个1D值数组。我想将该数组存储到CSV文件中。每次调用函数时,我都希望将此数据存储到CSV中的新列中。现在,我的代码处于半工作状态,每次调用该函数时,它都会不断地将新的数据数组添加到csv中,但它会将其添加到同一列的底部。我希望数据存储在下一列中,而不是添加到第一列的底部。任何帮助都将不胜感激

*我已经删除了生成标准\u dev数组的代码部分*

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

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“functions.h”
使用名称空间cv;
使用名称空间std;
无效标准偏差线(Mat图像、int列计数){
整数和=0;
int-standard_-dev_-col=0;
int i,j;
常数int cols=3840;
int rows=image.rows;
浮动标准差[cols];
浮动平均值[cols];
流式输出(“SD.csv”,ios::app);
对于(i=0;iout问题在于for循环中的endl,它输出一个换行符

爱德:试试这个

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;
}
模板
串连接(Iter p0,Iter p1){
std::字符串结果{};
如果(p0!=p1){
结果=std::to_字符串(*p0++);
}
而(p0!=p1){
结果+=',';
结果+=std::to_字符串(*p0++);
}
返回结果;
}
然后在不使用for循环的情况下使用它,如下所示:

out << join(standard_dev, standard_dev + cols) << '\n';

out还不如告诉询问者应该输出什么来结束回答:逗号。@user4581301所以当我用“,”替换endl时它只是在我的csv中创建了一行。所有内容都被覆盖,它不会像我希望的那样在列中打印出来。你如何知道一行何时完成?@user4581301每次通过for循环时,它都会创建一个新行。我第一次调用该函数时,它会将所有值放在列0中,并下拉一个新的值第二次调用函数时,我希望它从第1列开始。作为一种临时解决方法,我只是第一次调用函数时将第0行中的所有内容打印出来,然后在函数结束时打印出来