JY078
JY078

Reputation: 403

C++ file reading error

So my text.txt looks like this:

208 84  Sally Goodnow       30:23   52  F   Lancaster
209 344 Scott Grady         30:28   42  M   Clinton
210 191 gail holland        30:36   52  F   worcester
211 43  Karen Hughes        30:45   46  F   Shirley
212 221 Edward m Powers Jr. 30:48   60  M   Clinton
213 173 Lisa Zancewicz      30:49   34  F   Clinton
214 186 Julianne Ryll       30:54   51  F   Clinton
215 245 Briana Gibson       30:54   27  F   Marlborough

my code to read the file looks like this:

int a;
int b;
string c;
string d;
string e;
string f;
string g;
string h;

string mystr;
int mymin;
int mysec;

int i;


int count=0;
while((infile>>a>>b>>c>>d>>e>>f>>g)&&getline(infile, h))

my code just breaks at line 212, I think it has to do with the name Edward m Powers Jr. (now I really hate this name, every time I look at it, lol). The name uses up my four strings, making h=60 M Clinton. Is is because string h cannot store integer 60 in it? I don't think that's the reason.

I'm doing the getline(infile, h) because I don't care about the name of town, Lancaster, Clinton are town names.

I tried to skip line 212, I did something like this:

string all
while(getline(infile, all)
{
   if(all.at[0]!='2'&&all.at[1]!='1'&&all.at[2]!='2')
   {
      infile>>a>>b>>c>>d>>e>>f>>g;
      getline(infile, h);

I'm doing this just to skip line 212, but it did not work. It just breaks at other lines.

Upvotes: 2

Views: 1227

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118292

It looks to me like at least half the input parsing questions here on stackoverflow.com have to do with the >> operator. I never liked using this operator. It has certain, rigid semantics affecting both input parsing and the input stream's error state, and unless the input matches the same semantics, using the >> operator will not work reliably.

And, adding insult to injury, in the real world the input is rarely a match for 100% of >>'s idiosyncrasies.

In this case, your problem is that you should not be using the >> operator at all. The >> operator is for parsing whitespace-delimited strings. You have correctly identified, more or less, where your problem is, now you just need to take one final step, to complete your training as a Jedi, and ditch the >> operator completely. You cannot easily use it to parse this kind of input.

Your input looks like a bunch of fixed-width fields. The fields always occupy the same character positions, on each line. That's what it looks to me. The >> operator is not meant to be used for parsing such input.

Rather, you should be reading the input one line at a time, using std::getline(), then using the substr() method to extract each field from its corresponding character positions. Then, for each individual field, trim the trailing whitespace, which is trivial.

P.S. You are not even using the >> operator on the right object. You're already using std::getline() to read each line of input, but then inexplicably use the >> operator on the input stream object anyway. That's wrong. You need to parse the line you have just read, rather than the following input in the file, by constructing a std::istringstream based on the line just read, then using the >> operator on that object. But, as I said, you should not be using the >> operator anyway...

Upvotes: 3

Related Questions