0

I have a csv with weather data and want to work with them a bit. It's only to get some expirience with c++. Because I found no way to upload a attachment, here is a screenshot how it looks and I paste some lines (in the hope the look is the same if you copy&paste it in a txt)

csv

STATIONS_ID;MESS_DATUM;QN_3;  FX;  FM;QN_4; RSK;RSKF; SDK;SHK_TAG;  NM; VPM;  PM; TMK; UPM; TXK; TNK; TGK;eor
       4931;20170203;   10;   4.3;   1.3;    3;   0.2;   6;    2.133;-999;   6.4;   8.1;  961.43;    5.2;   91.88;    9.9;    2.5;   -2.7;eor
       4931;20170204;   10;  14.6;   3.0;    3;   0.1;   6;    0.750;-999;   5.9;   6.8;  957.18;    4.6;   80.88;    8.6;   -0.5;   -4.8;eor
       4931;20170205;   10;   8.6;   1.8;    3;   0.0;   6;    1.717;-999;   6.0;   6.4;  957.66;    3.8;   80.29;    7.7;   -1.8;   -5.4;eor
       4931;20170206;   10;   6.2;   1.9;    3;   0.0;   0;    1.533;-999;   7.3;   6.6;  967.88;    3.7;   82.79;    6.2;    2.3;   -0.2;eor
       4931;20170207;   10;   4.6;   1.1;    3;   0.0;   6;    4.317;-999;   7.2;   6.5;  970.54;    3.4;   83.67;    6.3;    0.7;   -3.3;eor
       4931;20170208;   10;   6.2;   2.2;    3;   0.0;   6;    1.000;-999;   7.2;   5.9;  973.68;    2.4;   81.33;    5.5;   -1.6;   -5.5;eor
       4931;20170209;   10;   6.7;   2.8;    3;   0.0;   0;    0.167;-999;   7.3;   4.6;  976.07;   -0.9;   79.71;    0.5;   -3.4;   -8.4;eor
       4931;20170210;   10;   5.1;   1.8;    3;   0.0;   7;    0.433;-999;   6.8;   5.0;  972.74;   -0.2;   82.92;    2.2;   -2.9;   -7.9;eor
       4931;20170211;   10;   5.7;   1.4;    3;   0.0;   0;    6.833;-999;   2.8;   5.6;  972.43;    1.1;   85.25;    8.5;   -5.5;   -8.9;eor

Now my question:

Is there a, more or less, simple way to read the csv but put only the marked data in a (2-dimensional) array?

Cheers and hth. - Alf
  • 135,616
  • 15
  • 192
  • 304
TCLeone
  • 23
  • 3
  • "To get some experience with C++" - I would recommend to write your own C++ solution and ask here on SO, how it could have been made better. That is a definitive way to learn C++. – badola Aug 08 '18 at 16:17
  • Since you're learning and probably don't want to use a library, I'd start with one of the answers to [How can I read and parse CSV files in C++?](https://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c), and discard or not record the row data you do not want. Once you haver that up and running, you'll probably spot a couple things you can do to improve on it. If not, or you suspect your code can be even better, Code Review may be right for you. 1/2 – user4581301 Aug 08 '18 at 16:30
  • But first, read [through the section on Asking Questions in their site help](https://codereview.stackexchange.com/help/asking) and [How to get the best value out of Code Review - Asking Questions](https://codereview.meta.stackexchange.com/questions/2436/how-to-get-the-best-value-out-of-code-review-asking-questions) 2/2 – user4581301 Aug 08 '18 at 16:32
  • More to the point, ask here if you have *problems* writing your solution. If your code *works*, but you would like feedback on it, don't ask here. [Ask **here** instead](http://codereview.stackexchange.com). – WhozCraig Aug 08 '18 at 16:33
  • Search the internet for "c++ read file CSV struct". Essentially, model the row with a class or structure. Use a vector to contain the *database*. Remember to overload `operator>>` to read the structure from an `std::istream`. – Thomas Matthews Aug 08 '18 at 16:45
  • Speaking of *database*, you may want to research using an actual database, something like MySQL, SqlLite, or Postgres. Also, read up on "relational database theory". – Thomas Matthews Aug 08 '18 at 16:46
  • If you don't want to access an external database, you may want to use `std::map` to create *index tables*. The map would be `std::map` where the first type is the column type you want to search and the second is the type for an index into your `std::vector` database. This may be faster to search than a linear search (depends on the number of rows). – Thomas Matthews Aug 08 '18 at 16:48
  • To tie this to your question, if you model the row with a structure, you can access any field (a.k.a. column) of the structure, ignoring the other fields. You may find that reading an entire row is more efficient that reading partial rows (the time difference should be negligible or the ignoring should be slower). Who know, you may want to access the other columns in the future. – Thomas Matthews Aug 08 '18 at 16:51

0 Answers0