Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing .csv files from C++

Tags:

c++

csv

I'm trying to output some data to a .csv file and it is outputting it to the file but it isn't separating the data into different columns and seems to be outputting the data incorrectly.

    ofstream Morison_File ("linear_wave_loading.csv");         //Opening file to print info to     Morison_File << "Time Force(N/m)" << endl;          //Headings for file     for (t = 0; t <= 20; t++) {       u = sin(omega * t);       du = cos(omega * t);        F = (0.5 * rho * C_d * D * u * fabs(u)) + rho * Area * C_m * du;         cout << "t = " << t << "\t\tF = " << F << endl;       Morison_File << t;                                 //Printing to file       Morison_File << F;      }       Morison_File.close(); 

Time and Force(N/m) are in columns A and B respectively but the t and F values are both printing the first row.

What is the syntax to separate them to print t into column A and F into column B?

like image 445
user3460758 Avatar asked Aug 08 '14 10:08

user3460758


People also ask

What is CSV file in C?

Many software products that deal with numbers and calculations have the ability to output data into a Comma Separated Value (CSV) file. This format can be an effective way of transporting data between different programs, as it is readable and fairly easy to manipulate.

How do I write data into a CSV file?

Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.

How do I create a custom CSV file?

Using OpenOffice or Google Sheets Create your spreadsheet, adding information as you ordinarily would, and go to “File.” On OpenOffice Calc, you choose “Save As” and then edit the “Save as type” field in the same way as on Excel, only this time you choose “Text CSV” from the options that appear.


1 Answers

There is nothing special about a CSV file. You can create them using a text editor by simply following the basic rules. The RFC 4180 (tools.ietf.org/html/rfc4180) accepted separator is the comma ',' not the semi-colon ';'. Programs like MS Excel expect a comma as a separator.

There are some programs that treat the comma as a decimal and the semi-colon as a separator, but these are technically outside of the "accepted" standard for CSV formatted files.

So, when creating a CSV you create your filestream and add your lines like so:

#include <iostream> #include <fstream> int main( int argc, char* argv[] ) {       std::ofstream myfile;       myfile.open ("example.csv");       myfile << "This is the first cell in the first column.\n";       myfile << "a,b,c,\n";       myfile << "c,s,v,\n";       myfile << "1,2,3.456\n";       myfile << "semi;colon";       myfile.close();       return 0; } 

This will result in a CSV file that looks like this when opened in MS Excel:

Image of CSV file created with C++

like image 137
BHawk Avatar answered Sep 22 '22 09:09

BHawk