What file types can c++ write to?

I've been working on a large array. I think I've finally got the array itself working, but I'm unable to open the file in a readable way. I tried to write it to an Excel file (.xlsx) and it Excel failed to open it, saying something to the effect of "make sure your file's format matches the file extension". The array is also unreadable in Notepad (I've also tried to write it as a .txt file) , so I can't just copy/paste it over. Is c++ able to write/format data to Excel?

Let me know if I should post my code, and thanks.
C++ doesn't understand anything about file formats. You, as the programmer, need output the correct format using C++.

If you want your array in a spreadsheet than I suspect you could use CSV file format. That is quite easy to produce using C++ standard libraries.


value 1-1, value 1-2, value 1-3
value 2-1, value 2-2, value 2-3
...


Simply output each row as a list of values separated by a comma ',' and ending in a new-line '\n'.
Last edited on
c++ can write to any type of file. The file extension tells excel what the contents of you file look like. So what probably happened, is that what you've written into the file doesn't look like what the extension says it should look like, so Excel complains. For example .csv files should have columns separated by commas and rows separated by newlines(i think). If the data in your file didn't look like this, issues will arise when opening it. It's similar with a .xlsx. Might I also suggest you don't use a .xlsx format if you don't have to, as I doubt the data format is the most straight-forward. I would advise you open a .csv file in Notepad(or similar) to see what the format looks like, open it in excel to see how Excel displays it. Then write a program to write data which is similar(or identical to be safe) to what you've seen in Notepad. The file you write to should be a .csv for Excel to recognise it properly)
Last edited on
Ok, thanks guys- makes sense to try writing to a simpler format first. Just ran it again using .csv, no luck.

I'm trying to use

1
2
3
4
5
6
7
ofstream out("test.csv", ios::out | ios::binary);

/*bunch of stuff being calculated/loops etc
where I define what entry goes in [i][j] of my matrix...*/

out.write((char *) &matr[i][j], sizeof matr[i][j]);               
out.close();      


Is this the correct way to go about it?
No, the format is comma separated values so data has to be separated by commas. Just dumping the data there is not going to work.

1
2
3
4
5
6
7
8
for(int i=0; i < rows; i++)
{
  for(int j =0; j < cols; j++)
  {
    out << data@[i][j] << "," if not last col index otherwise "";
  }
  out << "\n";
}

The pseudo-code roughly outlines what you need to do.

Again to ring this point home: Just dumping the data won't work. And this applies to most formats
Last edited on
As I said before C++ has no idea what a CSV file looks like. You have to program that yourself.

out.write((char *) &matr[i][j], sizeof matr[i][j]); // How is this outputting elements separated by commas?

You have to output each and every element and you have to output the comma that separates them and you have to output the end of line marker '\n'.
Got it. The format you gave for writing data makes much more sense than the one I'd tried to mimic.

edit: @Galik, yeah I noticed that after spaggy's response- seemed oh-so-obvious after that.

Thanks much for the help!
Last edited on
For writing a xls file, you should do this:

1
2
3
4
5
6
7
ofstream xls_Example("Test.xls", ios::out)
for(int i = 0; i < 10; i++)
{
     for(int j = 0; j < 10; j++)
          xls_Example << (i + 1) * (j + 1) << '\t' ;
     xls_Example << endl ;
}


it will create for you a 10*10 ply chart.
for going to next row you should use endl .
for going to next column you should use '\t' .
Topic archived. No new replies allowed.