C++ 是否将二维阵列传输到csv文件?

C++ 是否将二维阵列传输到csv文件?,c++,matrix,C++,Matrix,我有一个叫做“temp_matrix”的矩阵,看起来像这样: 0.0,100.0,100.0,100.0, 0.0,45.1,60.6,66.2,0, 0.0,45.1,60.6,66.2,0, 0.0,100.0,100.0,100.0,0, …除了我的是20x20矩阵 我试过: ofstream excel_plate("plate.csv"); excel_plate.write(temp_matrix[0],20); excel_plate.close(); cout <<

我有一个叫做“temp_matrix”的矩阵,看起来像这样:

0.0,100.0,100.0,100.0,
0.0,45.1,60.6,66.2,0,
0.0,45.1,60.6,66.2,0,
0.0,100.0,100.0,100.0,0,
…除了我的是20x20矩阵

我试过:

ofstream excel_plate("plate.csv");
excel_plate.write(temp_matrix[0],20);
excel_plate.close();
cout << endl;
ofstream excel_plate("plate.csv");
excel_plate << temp_matrix[0], 20, 20;
excel_plate.close();
流excel_板(“plate.csv”)的
;
excel板写入(温度矩阵[0],20);
excel_plate.close();

cout最简单的方法如下(假设您可以使用c++11)

std::of stream out(“test.csv”);
用于(自动和行:临时矩阵){
用于(自动列:行)

因此,此代码包含一个函数,该函数应该能够将任何2d字符串数组传输到csv文件中

    #include <iostream>
    #include <fstream>
    #include<string>  
    
    /* This is for a string array, but if you want to use any other type, just replace
       'std::string' with int,double....*/
    
    template <size_t row, size_t col>
    void twodarray2csv(std::string(&array)[row][col], std::string filename)
    {
        std::ofstream myfile;
        myfile.open(filename);
    
    for (size_t i = 0; i < row; ++i)
    {
        for (size_t j = 0; j < col; ++j)
            if (j < (col - 1)) {
                myfile << array[i][j] << ",";
            }
            else if (j == (col - 1)) {
                myfile << array[i][j] << "\n";
            }
    }
}
 
#包括
#包括
#包括
/*这是针对字符串数组的,但是如果您想使用任何其他类型,只需替换
带int的'std::string',双精度*/
模板
void twodarray2csv(std::string(&array)[row][col],std::string文件名)
{
std::流myfile;
myfile.open(文件名);
对于(尺寸i=0;imyfile你被否决了,因为你没有发布任何可远程编译的内容,
temp_矩阵
的定义在哪里?你似乎还相信
神奇地知道如何以某种方式格式化你的输出,
所做的就是将字节转储到文件中,如果
temp_矩阵
是一个2d数组,那么你就可以可能有未定义的行为。将您的问题重新格式化为可以复制和编译的内容,请参阅。感谢您的回复。我尝试将代码粘贴到此处,但程序中的注释将此网站的代码格式搞砸。我将重试。这是大量代码,我将尝试查看,但请记住下一步我决定最初不发布整个内容,因为同样的原因——它有很多代码,我不认为人们需要看到它来回答手头的问题。我唯一需要帮助的部分是从“ofstream”开始的主声明。哦,漂亮!你是个很棒的人。我不知道我们得到CSV文件的方法有多重要,只要我们得到它。如果你有动机/心,你认为你能快速显示一个普通的C++代码片断会是什么样子吗?我可能更熟悉?哦,没关系……这是显而易见的。gh。再次感谢!:](人们应该为我投票支持这个家伙!)你能为这个仅代码的答案添加一点上下文吗?
std::ofstream out("test.csv");

for (auto& row : temp_matrix) {
  for (auto col : row)
    out << col <<',';
  out << '\n';
}
    #include <iostream>
    #include <fstream>
    #include<string>  
    
    /* This is for a string array, but if you want to use any other type, just replace
       'std::string' with int,double....*/
    
    template <size_t row, size_t col>
    void twodarray2csv(std::string(&array)[row][col], std::string filename)
    {
        std::ofstream myfile;
        myfile.open(filename);
    
    for (size_t i = 0; i < row; ++i)
    {
        for (size_t j = 0; j < col; ++j)
            if (j < (col - 1)) {
                myfile << array[i][j] << ",";
            }
            else if (j == (col - 1)) {
                myfile << array[i][j] << "\n";
            }
    }
}
 
int main() {
        //example
        std::string ArrayName[9][3];
        /* this for loop is just there to populate the array, if you have an array
         already skip this step */
            #include <iostream>
        #include <fstream>
        #include<string>  
        
        /* This is for a string array, but if you want to use any other type, just replace
           'std::string' with int,double....*/
        
        template <size_t row, size_t col>
        void twodarray2csv(std::string(&array)[row][col], std::string filename)
        {
            std::ofstream myfile;
            myfile.open(filename);
        
        for (size_t i = 0; i < row; ++i)
        {
            for (size_t j = 0; j < col; ++j)
                if (j < (col - 1)) {
                    myfile << array[i][j] << ",";
                }
                else if (j == (col - 1)) {
                    myfile << array[i][j] << "\n";
                }
        }
    }
 int number = 0;
                for (int i = 0; i < 9; i++) {
                    for (int x = 0; x < 3; x++) {
            
                        ArrayName[i][x] = number;
                        number++;
                    }
                }
            
            
                //this part converts the array to a csv file of desried name
                twodarray2csv(ArrayName, "outputfile.csv");
            
            
            
            
            
                return 0;
            }