Excel or CSV output help

I'm attempting to output float values from a two dimensional array into either a csv or Excel file. The long term goal actually involves exporting multiple two dimensional arrays, but let's start with the basics. Perusing the forum, I was able to come up with this code based on a thread that had been inactive for quite some time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>

template< typename t_data>
void write_csv(t_data *a,int nrows,int ncols,std::ostream &outs)
{
    for (int row=0;row<nrows;row++)
    {
        for (int col=0;col<ncols;col++)
        {
            outs<<*a;
            if (col<ncols-1)
                outs<<", ";
            a++;
        }
        outs<<std::endl;
    }
}
int main()
{
    int a[5][10];
    for (int row=0;row<5;row++)
        for (int col=0;col<10;col++)
        a[row][col]=(row*100)+col;
    write_csv(a[0],5,10,std::cout);
}

While it's output appears to work correctly, I am unable to locate or name the csv file that is supposedly being created. The old thread no longer allowed posts or replies, so creating another thread seems the best way to approach the topic. Alternately, if there is an easier way to export data to actual xls or xlsx files, that would be a perfectly acceptable solution too. Thanks.
In your example, the output goes to std::cout - that is it is displayed on the screen.
If you want an output file, first create one, then specify it in the function call:
1
2
3
    std::ofstream fout("output.csv");
    
    write_csv(a[0],5,10,fout);
Excellent, thanks Chervil. It turns out that the plan for this program, unfortunately, may require Excel after all. At work, we are trying to get a program together to collect data about some electronic equipment. While another more experienced programmer is working on a portion of it, I needed to look into a way to get this data stored in an organized fashion. Because there is going to be an awful lot of it, I wasn't able to convince them that csv could do the job. Most of the code is actually being created in low level c, but this still was a helpful question to ask because there are a variety of uses for csv stored data. I understand actually working with Excel directly is much more difficult. I've seen some examples of working with it, but attempts to port that functionality into my own program have failed.
Last edited on
I guess it really depends on what the data is, and how it is organised. The thing about formats such as CSV is that they can be a kind of universal way of communication between programs, while proprietary formats such as Excels own files may be more tricky to handle.

Still, there isn't enough information here to advise on which format is most suitable, but it sounds as though your colleagues are more certain of what will and will not work.
It's because they want the automated organizational features available through excel, such as the use of graphs, charts and borders. The data is being collected from an error detector and oscilloscope capable of making fairly precise measurements, and communicating with the computer via a PCI-GPIB card. In short, we are altering the signal delay and voltage in small increments (like x and y in a graph), and storing the values to determine when errors occurred. There is more involved, but in a nutshell the same exercise is being performed on incrementing frequency ranges. Through the collection of this data, we hope to develop a calibration table that gives us the voltage and delay values appropriate for error free operation at a given input frequency.
they want the automated organizational features available through excel, such as the use of graphs, charts and borders.


Perhaps the one way to approach this would be to import the plain CSV data and then run an Excel macro to carry out these other tasks. That will provide the required automation at the click of a button (or press of a key).
Topic archived. No new replies allowed.