Exporting Char Array to Excel

Hello, I am a new programmer with the C++ language. I have a task at hand which I would love to get some help on:

I have successfully outputted 40,000 voltage points from a oscilloscope into a char type array in my program. Each voltage value is separated by commas. I wish to export this entire array into an excel file (a column!).

Also I wish to access this excel file over and over again in the future to dump more measurement readings (40,000 points each) into, lets say 2 columns over.

I suspect creating a csv file is the best way to go, but please let me know! Any help is much appreciated!

Thanks
You're probably better off using CSV.

As far as I know there is no publicly documented specification for the Excel file format (it keeps changing anyway). Even if you could manage this, there's still the risk of stepping on an MS patent.
closed account (z05DSL3A)
If you are interested in the file formats, they can be found here:
http://www.microsoft.com/interop/docs/OfficeBinaryFormats.mspx
Last edited on
If I understand you correctly, you wish to dump your array as a single column as
1
2
3
4
5

and later dump another array as a new column:
1, 100
2, 101
3, 102
4, 103
5, 104

Correct?

This can be done easily enough, but you cannot simply append to the existing file. You have to create a new file, then delete the old file when you are done.

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <algorithm>
#include <cstdio>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

bool dump_column( const string& filename, vector <double> data )
  {
  string tempname = filename.erase( filename.find_last_of( '.' ) ).append( ".tmp" );

  // We'll -first- rename the original data file to a temporary name. That way,
  // if anything goes wrong we'll still have at least the original data.
  if (rename( filename.c_str(), tempname.c_str() ) != 0) return false;

  ifstream inf( tempname.c_str() );
  if (!inf) return false;

  ofstream outf( filename.c_str(), ios::trunc );
  if (!outf) { inf.close(); return false; }

  int num_commas = 0;

  try {
    // Append the new data to the end of each line in the file
    string line;
    vector <double> ::iterator datum = data.begin();
    while (getline( inf, line ))
      {
      // EOD but not EOF
      if (datum == data.end()) break;

      // skip blank lines
      if (line.find_first_not_of( " \f\n\r\t\v" ) == string::npos) continue;

      // we might need this later
      if (num_commas == 0) num_commas = count( line.begin(), line.end(), ',' );

      // append new datum to line
      outf << line << ", " << *datum << endl;

      // next datum
      datum++;
      }

    // If we hit EOF before we ran out of data, we'll have to tack on
    // a whole bunch of empty fields to line the columns up.
    if (datum != data.end())
      {
      string empties( num_commas +1, ',' );
      while (datum != data.end())
        {
        outf << empties << ' ' << *datum << endl;
        datum++;
        }
      }

    // Don't forget old data if there is less new data than the old...
    else while (getline( inf, line )) outf << line << endl;

    }
  catch (...)
    {
    // (make sure to close the files if something really bad happens)
    outf.close();
    inf.close();
    throw;
    }

  outf.close();
  inf.close();

  // unlink the old data
  remove( tempname.c_str() );

  return true;
  }


This returns false if anything goes wrong with the files.

If anything at all does go wrong (including unforeseen exceptions), look for your file with the extension '.tmp'. It contains the original data. If it isn't there, then the original file remains unmodified.

The files are assumed to be in typical CSV format: commas separate values, newlines separate records, and all other whitespace is ignored. Blank lines are also ignored. There are no field delimiters or quote characters -- so commas and newlines cannot appear in a value.

Sorry this is a little more than simple 'help', but sometimes a good working example is better. You can see how it works and play around with it to suit your purposes.

BTW, I haven't actually compiled this... It should work but I could have typoed somewhere...

Good luck.
Topic archived. No new replies allowed.