将2D数组传输到csv文件?

vel*_*oon 1 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,
Run Code Online (Sandbox Code Playgroud)

...除了我的是20x20矩阵.

我试过了:

ofstream excel_plate("plate.csv");
excel_plate.write(temp_matrix[0],20);
excel_plate.close();
cout << endl;
Run Code Online (Sandbox Code Playgroud)

但无法编译.

我试过了:

ofstream excel_plate("plate.csv");
excel_plate << temp_matrix[0], 20, 20;
excel_plate.close();
Run Code Online (Sandbox Code Playgroud)

...但是只需在第一个单元格中获取带有一串字符的csv文件,如下所示:0052EE98.

这是我的代码:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <math.h>
#include <Windows.h>
#include <algorithm>
#include <string>
#include <array>
#undef max

using namespace std;

const double neighbors = 4;

// START FUNCTION: Update Elements
void average(double *temp_matrix, int ROWS, int COLUMNS)
{
    int i = 0;
    int j = 0;

    double row_left = 0;
    double row_right = 0;
    double column_up = 100;
    double column_down = 0;
    double sum = 0;

    double av = 0;

    for (int i = 1; i < ROWS - 1; i++)
    {
        for (int j = 1; j < COLUMNS - 1; j++)
        {
            /*
            cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
            cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
            cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
            cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
            */

            row_left = *(temp_matrix + i*ROWS + (j-1));
            row_right = *(temp_matrix + i*ROWS + (j + 1));
            column_up = *(temp_matrix + (i-1)*ROWS + j);
            column_down = *(temp_matrix + (i+1)*ROWS + j);

            sum = row_left + row_right + column_up + column_down;
            av = sum / neighbors;

            *(temp_matrix + i*ROWS + j) = av;
        }
    }
}
// END FUNCTION: Update Elements

// START FUNCTION: Updtate Until Stable
void update(double *temp_matrix, int ROWS, int COLUMNS)
{
    int i = 0;
    int j = 0;

    double row_left = 0;
    double row_right = 0;
    double column_up = 100;
    double column_down = 0;
    double sum = 0;

    double old_num = 0;
    double new_num = 0;

    double difference = 0;
    double max_change = .11;

    while (max_change > .1)
    {
        max_change = -1;
        for (int i = 1; i < ROWS - 1; i++)
        {
            for (int j = 1; j < COLUMNS - 1; j++)
            {
                old_num = *(temp_matrix + i*ROWS + j);

                /*
                cout << "row_left:" << *(temp_matrix + i*ROWS + (j - 1)) << " ";
                cout << "row_right:" << *(temp_matrix + i*ROWS + (j + 1)) << " ";
                cout << "column_up:" << *(temp_matrix + (i - 1)*ROWS + j) << " ";
                cout << "column_down:" << *(temp_matrix + (i + 1)*ROWS + j) << " ";
                */

                row_left = *(temp_matrix + i*ROWS + (j - 1));
                row_right = *(temp_matrix + i*ROWS + (j + 1));
                column_up = *(temp_matrix + (i - 1)*ROWS + j);
                column_down = *(temp_matrix + (i + 1)*ROWS + j);

                sum = row_left + row_right + column_up + column_down;
                new_num = sum / neighbors;

                difference = new_num - old_num;
                if (difference > max_change)
                {
                    max_change = difference;
                }

                *(temp_matrix + i*ROWS + j) = new_num;
            }
        }
    }
}
// END FUNCTION: Update Until Stable

// START FUNCTION: Print Array
void print_array(double *temp_matrix, int ROWS, int COLUMNS)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            if (j > 0)
            {
                cout << ",";
            }
                cout << fixed << setw(5) << setprecision(1) << *(temp_matrix + i*ROWS + j);
        }
        cout << endl;
    }
}
// END FUNCTION: Print Array

// START FUNCTION: Print 4 Excel
void print_excel(double *temp_matrix, int ROWS, int COLUMNS)
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            cout << fixed << setprecision(1) << *(temp_matrix + i*ROWS + j) << ",";
        }
        cout << endl;
    }
}
// END FUNCTION: Print 4 Excel

int main()
{

    const int ROWS = 20;
    const int COLUMNS = 20;

    double temp_matrix[ROWS][COLUMNS] =
    {
        { 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100, 0 }
    };


    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    average(temp_matrix[0], 20, 20);
    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    update(temp_matrix[0], 20, 20);
    print_array(temp_matrix[0], 20, 20);

    system("pause");
    cout << endl << endl;

    print_excel(temp_matrix[0], 20, 20);

    ofstream excel_plate("plate.csv");
    if (excel_plate.is_open())
    {
        excel_plate << temp_matrix[0], 20, 20;
        excel_plate.close();
    }
    else
    {
        cout << "Unable to open file.";
    }

    /*
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            if (j > 0)
            {
                cout << ",";
            }
            cout << temp_matrix[i][j];
        }
        cout << endl;
    }
    */

    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这应该非常非常简单.请帮助一个菜鸟.:)

PS因为这是一个作业,你会注意到我打印出几个矩阵.我打印出的最后一个是矩阵需要传输到.csv文件的格式.

use*_*267 8

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

std::ofstream out("test.csv");

for (auto& row : temp_matrix) {
  for (auto col : row)
    out << col <<',';
  out << '\n';
}
Run Code Online (Sandbox Code Playgroud)