0

Here's a piece of code I use to import my data from a data.csv file, into 24 variables:

#include <iostream>
#include <fstream>
using namespace std;

double xa = 0;
double ya = 0;
double Ta = 0;
double Ca = 0;
double Ma = 0;
double Da = 0;

double xb = 0;
double yb = 0;
double Tb = 0;
double Cb = 0;
double Mb = 0;
double Db = 0;

double xc = 0;
double yc = 0;
double Tc = 0;
double Cc = 0;
double Mc = 0;
double Dc = 0;

double xd = 0;
double yd = 0;
double Td = 0;
double Cd = 0;
double Md = 0;
double Dd = 0;


//__________________________________________________

int trash    = 0;   // ifstream bug (?) stuff

//___________________________________

int main() {


    ifstream ifs ("data.csv"); ///LOADING
if (!ifs)
    // process error
ifs >>  trash;
ifs >>  xa ;
ifs >>  ya ;
ifs >>  Da ;
ifs >>  Ma ;
ifs >>  Ca ;
ifs >>  Ta ;
ifs >>  xb ;
ifs >>  yb ;
ifs >>  Db ;
ifs >>  Mb ;
ifs >>  Cb ;
ifs >>  Tb ;
ifs >>  xc ;
ifs >>  yc ;
ifs >>  Dc ;
ifs >>  Mc ;
ifs >>  Cc ;
ifs >>  Tc ;
ifs >>  xd ;
ifs >>  yd ;
ifs >>  Dd ;
ifs >>  Md ;
ifs >>  Cd ;
ifs >>  Td ;

Of course (I guess...,) it works only with this CSV structure (each line contains a new single data:)

63.64474122 
5.21472834  
0.40511019  
0.26155648  
0.00000000  
0.00000000
34.45902482 
15.62249852 
0.22086168  
0.00000000  
0.44580498  
0.00000000
82.11020306 
14.02709406 
0.50748794  
0.00000000  
0.00000000  
0.15917872
0.00000000  
0.00000000  
0.00000000  
0.00000000  
0.00000000  
0.00000000

For obvious practical reasons, I'd like to use this file structure for the CSV (tab-and-newlines-separated):

63,64474122 5,21472834  0,40511019  0,26155648  0,00000000  0,00000000
34,45902482 15,62249852 0,22086168  0,00000000  0,44580498  0,00000000
82,11020306 14,02709406 0,50748794  0,00000000  0,00000000  0,15917872
0,00000000  0,00000000  0,00000000  0,00000000  0,00000000  0,00000000

Could somebody help me there?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
adrienlucca.net
  • 677
  • 2
  • 10
  • 26

1 Answers1

1

Your code (as abysmal as it is) is supposed to work fine with both files. iostream input operators treat input as a stream, which means that for types like int or double they don't care how the values are separated (by newlines or by blanks).

Now, taking a closer look, I see that the second file uses commas instead of decimal points. There are 3 ways you can handle this:

  • You can force the locale to be one that treats comma as a decimal point (see this question)
  • You can preprocess file using a script before reading it by your program.
  • Finally, you can read each number into an std::string variable first and then using std::istringstream convert it to double.

In any case, you should seriously consider reading up on iostream usage. At the very least you should check for input errors. And no, there is no such thing as "ifstream bug".

Community
  • 1
  • 1
Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • Well yes, I'm the bug, I know... However, with the second file structure (tabulations,) this code doesn't works. You mention "newlines" and "blanks" as separators, it doesn't make it with blanks as well... Sorry for being such a wild cryptic C++ user, that's really not my profession... – adrienlucca.net Nov 05 '13 at 04:04
  • 1
    Please see the clarification above. – Alexander L. Belikoff Nov 05 '13 at 04:09
  • thank you, it's my mistake... the file I tested used decimal points (not commas) and didn't load properly... but it's good to know I can use commas... – adrienlucca.net Nov 05 '13 at 05:05