A quick example code to write data to a csv file in C++

Writing data output files in C++ is a little more complicated than doing so in languages like Python or Matlab. I’ve been learning some basic C++ this summer and I thought I’d share a simple example code for writing data to a CSV file.  See the code below, as well as some resources I’ve found helpful.

 

// write_csv.cpp
// updated 8/12/16

// the purpose of this code is to demonstrate how to write data
// to a csv file in C++

// inlcude iostream and string libraries
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// create an ofstream for the file output (see the link on streams for
// more info)
ofstream outputFile;
ofstream fs
// create a name for the file output
std::string filename = "exampleOutput.csv";

// create some variables for demonstration
int i;
int A;
int B;
int C;

int main(int argc, char** argv)
{
    // create and open the .csv file
    fs.open(outputFile,filename);
    
    // write the file headers
    outputFile << "Column A" << "," << "Column B" << "Column C" << std::endl;
    
    // i is just a variable to make numbers with in the file
    i = 1;
  
    // write data to the file
    for (int counter = 0; counter <  10; counter++)
    {
        // make some data
        A = i + 5;
        B = i + 10;
        C = i + 20;
        
        // write the data to the output file
        outputFile << A << "," << B << "," << C << std::endl;
        
        // increase i
        i = i * 5;
    }
    
    // close the output file
    outputFile.close();
    return 0;   
}

Helpful resources on writing data outputs in C++:

Basic info on input and output in C++: http://www.cplusplus.com/doc/tutorial/basic_io/

Short tutorial on writing txt file output: http://www.cplusplus.com/doc/tutorial/files/

Background on streams: http://www.cprogramming.com/tutorial/c++-iostreams.html

A nice tutorial from BU: https://www.cs.bu.edu/teaching/cs111/spring-2000/file-io/

 

 

 

 

 

 

Advertisements

4 thoughts on “A quick example code to write data to a csv file in C++

  1. Why are you using global variables? Generally acknowledged as bad coding practice. And you use variable outFile when I believe you meant outputFile. Compile your code before posting and always clear warnings.

    Dr. Robert Reynolds from Wayne State University in Detroit, MI has done some great work in the area of genetic algorithms. Have you looked at any of his research?

    • Hi Bob, thanks for the comment. I’m in the process of learning to code in C++, so my coding practices are still a work in progress. Any tips help, so thank you. I can’t say I’m familiar with Dr. Reynolds, I’ll have to take a look at his work.

  2. Pingback: Reading CSV files in C++ – Water Programming: A Collaborative Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s