Advance reading from file

Hi! I tried to find an answer, but I couldn't find anything on this. If I need to read from file such line:

2017-10-11,2550.620117,2555.239990,2547.949951,2555.239990,2555.239990,2976090000

and save numbers as: string(date), double, double, double, double, double and long (so each number after comma should be separate variable)

How should I do this?

getline() accepts string only, and every time when I try to save each number separate string reads the whole line anyway and rest values are garbage.

Thank you for any help!

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
26
27
28
29
30
31
32
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<string>
using namespace std;


int main()
{
   string line;
   string date;
   double d[5];
   char c;
   long long num;                                           // long long

   ifstream in( "data.txt" );
   while( getline( in, line ) )                             // read a line
   {
      stringstream ss( line );                              // set up a stringstream from the whole line
      getline( ss, date, ',' );                             // collect up to the comma as a date string
      for ( int i = 0; i < 5; i++ ) ss >> d[i] >> c;        // stream the doubles out, each followed by a char
      ss >> num;                                            // stream the long long integer out

      // Check data
      cout << "\nDate: " << date;
      cout << "\nDoubles: ";
      for ( int i = 0; i < 5; i++ ) cout << fixed << setprecision( 6 ) << d[i] << " ";
      cout << "\nLong long: " << num << "\n\n";
   }
   in.close();
}


You probably want to store the data from each line in a suitable struct, and the whole collection of these as a vector of those structs, but since I don't know what the numbers represent I'll just leave them as that.

data.txt:
2017-10-11,2550.620117,2555.239990,2547.949951,2555.239990,2555.239990,2976090000
2016-09-10,3661.731228,3666.340001,3658.050062,3666.349001,3666.340001,3087101111
2015-08-09,4772.842339,4777.451112,4769.161173,4777.450112,4777.451112,4198212222


Date: 2017-10-11
Doubles: 2550.620117 2555.239990 2547.949951 2555.239990 2555.239990 
Long long: 2976090000


Date: 2016-09-10
Doubles: 3661.731228 3666.340001 3658.050062 3666.349001 3666.340001 
Long long: 3087101111


Date: 2015-08-09
Doubles: 4772.842339 4777.451112 4769.161173 4777.450112 4777.451112 
Long long: 4198212222
Last edited on
There are some options. Choose one that seems easiest for you.

First option: read the whole line with getline, than replace all ',' with ' ' or '\t', create a stringstream with this line and use >> to read the values.

Second option: use getline to read date with separator ',' then read double with >> which terminates at ',' since you use '.' as decimal point then read the separator ',' and so on for all numerical values.

Hope this makes sense.
OOOOOOO, thank you for help so much :D problem is solved

And yeah I want to store each line in object called "line", but i will handle this
Topic archived. No new replies allowed.