University of Leeds

Writing CSV files

Introduction

When writing data to a file, it is important to use recognised file formats so that the data can be imported into other programs. One of the simplest and most common file format is comma-separated variable (CSV). CSV files often have the .csv extension and can be easily imported into Microsoft Excel for further processing or plotting.

Example

The example below writes one period of a sinusoid to a .csv file. The file has two columns, with the values separated by a comma. The first column is the x value (in radians) and the second column is sin(x).

main.cpp

 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
#include <cmath>
#include <fstream>
#include <iostream>

const double PI = 3.14159265359;

int main() {
  // create an output file stream
  std::ofstream output;
  // use it to open a file named 'output.csv'
  output.open("output.csv");
  // check if the file is not open
  if (!output.is_open()) {
    // print error message and quit if a problem occurred
    std::cerr << "Error creating file!\n";
    exit(1);
  }

  // loop through values 0 to 2PI in small steps
  for (double x = 0.0; x <= 2 * PI; x += 0.01) {
    // print x and sin(x) in CSV format
    output << x << "," << sin(x) << std::endl;
  }
  output.close();
}

The code creates an output file stream and opens a file 'output.csv'. Note the use of the .csv extension. Doing this means that the file will be associated by the operating system with a program that can handle .csv files (usually Excel). Once it has checked whether it has been successfully created, it loops through the values in a single period (0 to 2π) and prints the x and sin(x) values to the output file. Note that a comma , is used to separate the variables.

Output

The example will create a file named 'output.txt' will the following contents.

0,0
0.01,0.00999983
0.02,0.0199987
0.03,0.0299955
0.04,0.0399893
0.05,0.0499792
0.06,0.059964
0.07,0.0699428
0.08,0.0799147
0.09,0.0898785
0.1,0.0998334
0.11,0.109778
0.12,0.119712
0.13,0.129634
0.14,0.139543
0.15,0.149438
0.16,0.159318
0.17,0.169182
0.18,0.17903
0.19,0.188859
0.2,0.198669
0.21,0.20846
0.22,0.21823
0.23,0.227978
0.24,0.237703
0.25,0.247404
0.26,0.257081
0.27,0.266731
0.28,0.276356
0.29,0.285952
0.3,0.29552
0.31,0.305059
0.32,0.314567
0.33,0.324043
0.34,0.333487
0.35,0.342898
0.36,0.352274
0.37,0.361615
0.38,0.37092
0.39,0.380188
0.4,0.389418
0.41,0.398609
0.42,0.40776
0.43,0.416871
0.44,0.425939
0.45,0.434966
0.46,0.443948
0.47,0.452886
0.48,0.461779
0.49,0.470626
0.5,0.479426
0.51,0.488177
0.52,0.49688
0.53,0.505533
0.54,0.514136
0.55,0.522687
0.56,0.531186
0.57,0.539632
0.58,0.548024
0.59,0.556361
0.6,0.564642
0.61,0.572867
0.62,0.581035
0.63,0.589145
0.64,0.597195
0.65,0.605186
0.66,0.613117
0.67,0.620986
0.68,0.628793
0.69,0.636537
0.7,0.644218
0.71,0.651834
0.72,0.659385
0.73,0.66687
0.74,0.674288
0.75,0.681639
0.76,0.688921
0.77,0.696135
0.78,0.703279
0.79,0.710353
0.8,0.717356
0.81,0.724287
0.82,0.731146
0.83,0.737931
0.84,0.744643
0.85,0.75128
0.86,0.757843
0.87,0.764329
0.88,0.770739
0.89,0.777072
0.9,0.783327
0.91,0.789504
0.92,0.795602
0.93,0.80162
0.94,0.807558
0.95,0.813416
0.96,0.819192
0.97,0.824886
0.98,0.830497
0.99,0.836026
1,0.841471
1.01,0.846832
1.02,0.852108
1.03,0.857299
1.04,0.862404
1.05,0.867423
1.06,0.872355
1.07,0.877201
1.08,0.881958
1.09,0.886627
1.1,0.891207
1.11,0.895699
1.12,0.9001
1.13,0.904412
1.14,0.908633
1.15,0.912764
1.16,0.916803
1.17,0.920751
1.18,0.924606
1.19,0.928369
1.2,0.932039
1.21,0.935616
1.22,0.939099
1.23,0.942489
1.24,0.945784
1.25,0.948985
1.26,0.95209
1.27,0.955101
1.28,0.958016
1.29,0.960835
1.3,0.963558
1.31,0.966185
1.32,0.968715
1.33,0.971148
1.34,0.973485
1.35,0.975723
1.36,0.977865
1.37,0.979908
1.38,0.981854
1.39,0.983701
1.4,0.98545
1.41,0.9871
1.42,0.988652
1.43,0.990105
1.44,0.991458
1.45,0.992713
1.46,0.993868
1.47,0.994924
1.48,0.995881
1.49,0.996738
1.5,0.997495
1.51,0.998152
1.52,0.99871
1.53,0.999168
1.54,0.999526
1.55,0.999784
1.56,0.999942
1.57,1
1.58,0.999958
1.59,0.999816
1.6,0.999574
1.61,0.999232
1.62,0.99879
1.63,0.998248
1.64,0.997606
1.65,0.996865
1.66,0.996024
1.67,0.995083
1.68,0.994043
1.69,0.992904
1.7,0.991665
1.71,0.990327
1.72,0.98889
1.73,0.987354
1.74,0.985719
1.75,0.983986
1.76,0.982154
1.77,0.980224
1.78,0.978197
1.79,0.976071
1.8,0.973848
1.81,0.971527
1.82,0.969109
1.83,0.966594
1.84,0.963983
1.85,0.961275
1.86,0.958471
1.87,0.955572
1.88,0.952576
1.89,0.949486
1.9,0.9463
1.91,0.94302
1.92,0.939645
1.93,0.936177
1.94,0.932615
1.95,0.92896
1.96,0.925212
1.97,0.921371
1.98,0.917438
1.99,0.913413
2,0.909297
2.01,0.905091
2.02,0.900793
2.03,0.896406
2.04,0.891929
2.05,0.887362
2.06,0.882707
2.07,0.877964
2.08,0.873133
2.09,0.868215
2.1,0.863209
2.11,0.858118
2.12,0.85294
2.13,0.847678
2.14,0.84233
2.15,0.836899
2.16,0.831383
2.17,0.825785
2.18,0.820104
2.19,0.814341
2.2,0.808496
2.21,0.802571
2.22,0.796565
2.23,0.79048
2.24,0.784316
2.25,0.778073
2.26,0.771753
2.27,0.765355
2.28,0.758881
2.29,0.752331
2.3,0.745705
2.31,0.739005
2.32,0.732231
2.33,0.725384
2.34,0.718465
2.35,0.711473
2.36,0.704411
2.37,0.697278
2.38,0.690075
2.39,0.682803
2.4,0.675463
2.41,0.668056
2.42,0.660581
2.43,0.653041
2.44,0.645435
2.45,0.637765
2.46,0.630031
2.47,0.622234
2.48,0.614374
2.49,0.606454
2.5,0.598472
2.51,0.590431
2.52,0.582331
2.53,0.574172
2.54,0.565956
2.55,0.557684
2.56,0.549355
2.57,0.540972
2.58,0.532535
2.59,0.524044
2.6,0.515501
2.61,0.506907
2.62,0.498262
2.63,0.489567
2.64,0.480823
2.65,0.472031
2.66,0.463191
2.67,0.454306
2.68,0.445375
2.69,0.436399
2.7,0.42738
2.71,0.418318
2.72,0.409214
2.73,0.400069
2.74,0.390885
2.75,0.381661
2.76,0.372399
2.77,0.3631
2.78,0.353764
2.79,0.344393
2.8,0.334988
2.81,0.325549
2.82,0.316078
2.83,0.306575
2.84,0.297041
2.85,0.287478
2.86,0.277886
2.87,0.268266
2.88,0.258619
2.89,0.248947
2.9,0.239249
2.91,0.229528
2.92,0.219784
2.93,0.210017
2.94,0.20023
2.95,0.190423
2.96,0.180596
2.97,0.170752
2.98,0.16089
2.99,0.151013
3,0.14112
3.01,0.131213
3.02,0.121293
3.03,0.111361
3.04,0.101418
3.05,0.0914646
3.06,0.0815022
3.07,0.0715315
3.08,0.0615537
3.09,0.0515698
3.1,0.0415807
3.11,0.0315874
3.12,0.021591
3.13,0.0115924
3.14,0.00159265
3.15,-0.00840725
3.16,-0.0184063
3.17,-0.0284035
3.18,-0.0383979
3.19,-0.0483884
3.2,-0.0583741
3.21,-0.068354
3.22,-0.078327
3.23,-0.0882922
3.24,-0.0982486
3.25,-0.108195
3.26,-0.118131
3.27,-0.128055
3.28,-0.137966
3.29,-0.147863
3.3,-0.157746
3.31,-0.167612
3.32,-0.177462
3.33,-0.187295
3.34,-0.197108
3.35,-0.206902
3.36,-0.216675
3.37,-0.226427
3.38,-0.236155
3.39,-0.245861
3.4,-0.255541
3.41,-0.265196
3.42,-0.274825
3.43,-0.284426
3.44,-0.293998
3.45,-0.303542
3.46,-0.313054
3.47,-0.322536
3.48,-0.331985
3.49,-0.341401
3.5,-0.350783
3.51,-0.36013
3.52,-0.369441
3.53,-0.378715
3.54,-0.387951
3.55,-0.397148
3.56,-0.406306
3.57,-0.415423
3.58,-0.424498
3.59,-0.433531
3.6,-0.44252
3.61,-0.451466
3.62,-0.460366
3.63,-0.46922
3.64,-0.478027
3.65,-0.486787
3.66,-0.495497
3.67,-0.504159
3.68,-0.512769
3.69,-0.521329
3.7,-0.529836
3.71,-0.538291
3.72,-0.546691
3.73,-0.555037
3.74,-0.563327
3.75,-0.571561
3.76,-0.579738
3.77,-0.587857
3.78,-0.595917
3.79,-0.603918
3.8,-0.611858
3.81,-0.619737
3.82,-0.627554
3.83,-0.635308
3.84,-0.642999
3.85,-0.650625
3.86,-0.658186
3.87,-0.665682
3.88,-0.673111
3.89,-0.680473
3.9,-0.687766
3.91,-0.694991
3.92,-0.702146
3.93,-0.709231
3.94,-0.716246
3.95,-0.723188
3.96,-0.730058
3.97,-0.736856
3.98,-0.743579
3.99,-0.750228
4,-0.756802
4.01,-0.763301
4.02,-0.769723
4.03,-0.776068
4.04,-0.782336
4.05,-0.788525
4.06,-0.794636
4.07,-0.800667
4.08,-0.806618
4.09,-0.812488
4.1,-0.818277
4.11,-0.823984
4.12,-0.829609
4.13,-0.835151
4.14,-0.840609
4.15,-0.845984
4.16,-0.851273
4.17,-0.856478
4.18,-0.861597
4.19,-0.86663
4.2,-0.871576
4.21,-0.876435
4.22,-0.881206
4.23,-0.885889
4.24,-0.890484
4.25,-0.894989
4.26,-0.899405
4.27,-0.903732
4.28,-0.907967
4.29,-0.912112
4.3,-0.916166
4.31,-0.920128
4.32,-0.923998
4.33,-0.927776
4.34,-0.931461
4.35,-0.935053
4.36,-0.938551
4.37,-0.941955
4.38,-0.945266
4.39,-0.948481
4.4,-0.951602
4.41,-0.954628
4.42,-0.957558
4.43,-0.960392
4.44,-0.963131
4.45,-0.965773
4.46,-0.968319
4.47,-0.970767
4.48,-0.973119
4.49,-0.975373
4.5,-0.97753
4.51,-0.979589
4.52,-0.98155
4.53,-0.983413
4.54,-0.985178
4.55,-0.986844
4.56,-0.988411
4.57,-0.98988
4.58,-0.991249
4.59,-0.99252
4.6,-0.993691
4.61,-0.994763
4.62,-0.995735
4.63,-0.996608
4.64,-0.997381
4.65,-0.998054
4.66,-0.998628
4.67,-0.999102
4.68,-0.999476
4.69,-0.999749
4.7,-0.999923
4.71,-0.999997
4.72,-0.999971
4.73,-0.999845
4.74,-0.999619
4.75,-0.999293
4.76,-0.998867
4.77,-0.998341
4.78,-0.997715
4.79,-0.99699
4.8,-0.996165
4.81,-0.99524
4.82,-0.994216
4.83,-0.993092
4.84,-0.991869
4.85,-0.990547
4.86,-0.989125
4.87,-0.987605
4.88,-0.985986
4.89,-0.984269
4.9,-0.982453
4.91,-0.980538
4.92,-0.978526
4.93,-0.976416
4.94,-0.974208
4.95,-0.971903
4.96,-0.969501
4.97,-0.967001
4.98,-0.964405
4.99,-0.961713
5,-0.958924
5.01,-0.95604
5.02,-0.95306
5.03,-0.949984
5.04,-0.946814
5.05,-0.943549
5.06,-0.940189
5.07,-0.936736
5.08,-0.933189
5.09,-0.929548
5.1,-0.925815
5.11,-0.921989
5.12,-0.91807
5.13,-0.91406
5.14,-0.909959
5.15,-0.905767
5.16,-0.901484
5.17,-0.897111
5.18,-0.892648
5.19,-0.888096
5.2,-0.883455
5.21,-0.878725
5.22,-0.873908
5.23,-0.869004
5.24,-0.864012
5.25,-0.858934
5.26,-0.853771
5.27,-0.848522
5.28,-0.843188
5.29,-0.837769
5.3,-0.832267
5.31,-0.826682
5.32,-0.821014
5.33,-0.815264
5.34,-0.809433
5.35,-0.80352
5.36,-0.797527
5.37,-0.791455
5.38,-0.785303
5.39,-0.779073
5.4,-0.772764
5.41,-0.766379
5.42,-0.759917
5.43,-0.753379
5.44,-0.746765
5.45,-0.740077
5.46,-0.733315
5.47,-0.72648
5.48,-0.719572
5.49,-0.712592
5.5,-0.70554
5.51,-0.698418
5.52,-0.691227
5.53,-0.683966
5.54,-0.676637
5.55,-0.66924
5.56,-0.661776
5.57,-0.654246
5.58,-0.646651
5.59,-0.638991
5.6,-0.631267
5.61,-0.62348
5.62,-0.61563
5.63,-0.607719
5.64,-0.599747
5.65,-0.591716
5.66,-0.583625
5.67,-0.575475
5.68,-0.567269
5.69,-0.559005
5.7,-0.550686
5.71,-0.542311
5.72,-0.533882
5.73,-0.5254
5.74,-0.516865
5.75,-0.508279
5.76,-0.499642
5.77,-0.490955
5.78,-0.482218
5.79,-0.473434
5.8,-0.464602
5.81,-0.455724
5.82,-0.4468
5.83,-0.437832
5.84,-0.428819
5.85,-0.419764
5.86,-0.410667
5.87,-0.401529
5.88,-0.39235
5.89,-0.383133
5.9,-0.373877
5.91,-0.364583
5.92,-0.355254
5.93,-0.345888
5.94,-0.336488
5.95,-0.327055
5.96,-0.317589
5.97,-0.308091
5.98,-0.298562
5.99,-0.289003
6,-0.279415
6.01,-0.2698
6.02,-0.260157
6.03,-0.250489
6.04,-0.240795
6.05,-0.231078
6.06,-0.221337
6.07,-0.211574
6.08,-0.20179
6.09,-0.191986
6.1,-0.182163
6.11,-0.172321
6.12,-0.162462
6.13,-0.152587
6.14,-0.142697
6.15,-0.132792
6.16,-0.122874
6.17,-0.112944
6.18,-0.103002
6.19,-0.0930505
6.2,-0.0830894
6.21,-0.07312
6.22,-0.0631433
6.23,-0.0531602
6.24,-0.0431719
6.25,-0.0331792
6.26,-0.0231832
6.27,-0.0131849
6.28,-0.0031853

Importing .csv files

Many programs, including Excel, include the ability to import the data contained in .csv files. It is usually just a case of using the import command and then specifying the de-limiting character (in this case a comma). Once the data is in the spreadsheet it is simple to plot a graph.

sine wave