0

Hi so this code worked 5 minutes ago to do exactly what I needed it to do.

The data is:

Set Field [G],Sheet Resistivity (Gavg) [ohm/sqr]
0.0000E+0,
0.0000E+0,7.270620E+2
1.0000E-2,
1.0000E-2,7.271280E+2
-1.0000E-2,
-1.0000E-2,
-1.0000E-2,7.271290E+2

And my code for it is:

#include <iostream>
#include <fstream>

using namespace std;

int main(){

  ifstream ip("/Users/10Exahertz/Documents/Hall Data/Test/data.txt");

  if(!ip.is_open()) std::cout << "ERROR: File Open" << '\n';

  string x;
  string y;

  while(getline(ip,x,',')){
    getline(ip,y,'\n');

   if(y!="")
    std::cout <<x<<","<< y << '\n';

  }
  ip.close();
}

Like I said 5 mintues ago this worked, it got rid of the rows with an empty y string and all was good. But then I went back to the original data file and it didnt work there. I was confused so I put the original data into data.txt and now that one is not working either. Im honestly confused, but what would be the best condition in that if loop to make it so this works.

  • 1
    Just to be clear, are you trying to print only rows that have more than one comma separated value? – scohe001 Oct 04 '18 at 15:19
  • erm basically if the data says something like 0,blank I dont want that printed. If it says 1,2 so both entries are there, I want it printed. – Steven Alsheimer Oct 04 '18 at 15:23
  • 1
    Looks like this code should do that...what do you mean when you say "not working?" Is it printing every line regardless? Or no lines? Or some other weird combination? – scohe001 Oct 04 '18 at 15:25
  • 1
    Also are you sure that there will never be whitespace (like a space or tab) after the comma even if there's no second value? – scohe001 Oct 04 '18 at 15:26
  • It prints everyline, and nah there shouldnt be. My guess is that the string isnt empty, but it worked before on what I thought was the same data. – Steven Alsheimer Oct 04 '18 at 15:29

1 Answers1

1

It sounds like you may have some whitespace that's crept in. I'd use the solution from this answer to trim the whitespace from your y string just to be sure:

#include <iostream>
#include <algorithm> 
#include <cctype>
#include <locale>
#include <fstream>

using namespace std;

// trim from start (in place)
static inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
        return !std::isspace(ch);
    }));
}

int main() {
    [...]
    while(getline(ip,x,',')){
        getline(ip,y,'\n');
        ltrim(y);

        if(y!="")
            std::cout <<x<<","<< y << '\n';
    }
}
scohe001
  • 15,110
  • 2
  • 31
  • 51
  • gives me the following error: " error: expected expression s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {" ^ – Steven Alsheimer Oct 04 '18 at 15:47
  • 1
    @StevenAlsheimer I didn't include iostream or put `using namespace std;` in my example, but when I do, looks like it compiles fine for me: https://ideone.com/0SFocq – scohe001 Oct 04 '18 at 15:49
  • 1
    If you're using an older C++ standard, maybe you could try his "Original Answer" solution? Use that `ltrim` instead of the one I copied into here. – scohe001 Oct 04 '18 at 15:51
  • @StevenAlsheimer happy to help :) – scohe001 Oct 04 '18 at 16:07