0

Here is my code. When I use '\t' as delimiter with a dataset separated by \t it works, but when I use ',' with a dataset separated by ',' it does't works.

void TApp::LoadTree(char * fileName){
   ifstream in(fileName);
   char starID[200];
   double dRa,dDec,dU,dG,dR,dI,dZ;
   long w = 0;
   TStar * star;

   if (SlimTree!=NULL){

      if (in.is_open()){
         cout << "\nLoading objects ";
         while(in.getline(starID, 200, ',')){
            in >> dRa;
            in >> dDec;
            in >> dU;
            in >> dG;
            in >> dR;
            in >> dI;
            in >> dZ;
            in.ignore();
            star = new TStar(starID, dRa, dDec, dU, dG, dR, dI, dZ);
            SlimTree->Add(star);
            delete star;
            w++;
            if (w % 10 == 0){
               cout << '.';
            }//end if*/
         }//end while

The data file I am trying to read looks like this:

1237645876878180399,15.2062613727499,-1.02188584631684,-9999,-9999,-9999,-9999,-9999

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • 2
    Unrelated to your problem, but I recommend you stop using the stream `getline` member function, and instead use [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) together with [`std::string`](https://en.cppreference.com/w/cpp/string/basic_string). – Some programmer dude Oct 25 '18 at 17:44
  • Check the stream status after reading to make sure you *are* reading. – user4581301 Oct 25 '18 at 17:46
  • 1237645876878180399,15.2062613727499,-1.02188584631684,-9999,-9999,-9999,-9999,-9999 is not going to work how you are reading. There are many duplicates for reading csv files. – drescherjm Oct 25 '18 at 17:47
  • 1
    Unrelated: `star = new TStar(...); SlimTree->Add(star); delete star;` is unusual. If `Add(star)` stores the pointer, `delete star;` will invalidate the stored pointer. If `Add(star)` stores a copy of the pointed-at data you can `TStar star (...); SlimTree->Add(&star);` and save yourself from dynamically allocating. Plus the compiler may find some additional room for optimizing, especially if you change `Add` to accept a reference rather than a pointer. – user4581301 Oct 25 '18 at 17:52

1 Answers1

1

My guess is that you have a comma-separated values (i.e. a CSV) file, where the comma separates all fields.

Then your code won't work because the input operator >> don't know anything about any other separator than white-space.

It works with tabs (or other white-space) as separator because the >> operator skips leading white-space, and stops reading at white-space as well.


For CSV files I usually recommend that you find a library to help you read and parse them, because CSV files a deceptively simple, but in reality can contain many special and corner cases that makes reading them non-trivial.

If you still want to attempt to parse them yourself, then I really recommend that you first of all read full lines and attempt to parse each line separately. Then you have to remember to read the separator between every field.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • You can change the delimiter if you really, really want to (see e.g. https://stackoverflow.com/questions/7302996/changing-the-delimiter-for-cin-c) but I second your recommendations. – drRobertz Oct 25 '18 at 17:51
  • 1
    ***the dataset is on a .txt file*** It does not matter what the extension is. It's still a CSV file. – drescherjm Oct 25 '18 at 18:01