如何将此代码的输出直接写入.csv文件

How can I write the output of this code straight to .CSV file?

本文关键字:csv 文件 输出 代码      更新时间:2023-10-16

如何将此代码的输出直接写入.csv文件?我想将输出直接生成.csv文件。

感谢您的帮助!

我只想将所有这些组合放入.csv文件中。

请帮助。

#include <cstdlib>
#include <iostream>
using namespace std;
// Program to print all combination of size r in an array of size n
#include <stdio.h>
void combinationUtil(int arr[], int data[], int start, int end, 
                    int index, int r);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
    // A temporary array to store all combination one by one
    int data[r];
    // Print all combination using temprary array 'data[]'
    combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end,
                    int index, int r)
{
    // Current combination is ready to be printed, print it
    if (index == r)
    {
        for (int j=0; j<r; j++)
            printf("%d ", data[j]);
        printf("n");
        return;
    }
    // replace index with all possible elements. The condition
    // "end-i+1 >= r-index" makes sure that including one element
    // at index will make a combination with remaining elements
    // at remaining positions
    for (int i=start; i<=end && end-i+1 >= r-index; i++)
    {
        data[index] = arr[i];
        combinationUtil(arr, data, i+1, end, index+1, r);
    }
}
// Driver program to test above functions
int main()
{
    int arr[] = {1, 2, 3, 4, 5, 6};
    int r = 5;
    int n = sizeof(arr)/sizeof(arr[0]);
    printCombination(arr, n, r);
    system("PAUSE");
    return EXIT_SUCCESS;
}

实际上.csv文件是纯文本中存储的逗号分隔值。因此,您可以代替printf -ing只需在.csv文件中写输出即可。

示例:
而不是:printf("%d ", data[j]);printf("n");
写:csv_file << data[j] << ", ";csv_file << "n";

本教程将教您如何从文件中读写。

或(第二解决方案)您可以将逗号分隔的输出重定向到*csv文件。检查此。