Link to home
Start Free TrialLog in
Avatar of J_B
J_BFlag for United States of America

asked on

C++ Parsing a string from an input file


I am reading a file and setting each line to a string but I need to either parse this string or be able to read the file in a better way. (currently just reading each line as 1 long string into the string entireLine)

Currently using:
  getline(inClientFile,entireLine);

I would like to parse the entireLine string but not sure how.

There are 3 fields in each line of the file formated as follows:

field1, field2, "data for field 3"

The fields are separated by commas but the 3rd field that is enclosed in quotes may have commas inside of it.

Any help in parsing this field would be greatly appreciated.






Avatar of phoffric
phoffric

I am working on a parsing problem in C, but you may still get something out of it. (It will work in C++.)
    https://www.experts-exchange.com/questions/26296790/Parsing-data-coming-from-file-into-char-buffer-with-delimeters.html
Search on "parse".
Avatar of J_B

ASKER

Thanks! I am taking a break for dinner but will check it out soon.
Avatar of J_B

ASKER

I've made some progress. I am able to get the first field parsed as follows:

 char keys[] = ",";
 int i;
  i = strcspn (stringOne,keys);
         //Set field1 = substring up to i
          field1 = entireLine.substr (0,i)

Just need to get field2 and field3 now.
ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If any field may or may not be enclosed in quotes, then you may want to think about finding the first delimiter using both comma and quote; and if a quote is found, then the next delimiter is strictly quote (in order to avoid potential commas with the quote.
Avatar of J_B

ASKER

I am a little confused. Should I be doing a find_first_of on the commas in between the fields?

My concern with just searching on commas is that field3 may have commas within the field because field3 is enclosed in quotes.
Avatar of J_B

ASKER

You were reading my mind. I just saw your post after I hit submit.
>> You were reading my mind.
Shh, let's keep my little secret between you and me :)
I'll be back in about 2 hours to see if you have any follow-up questions. Feel free to post what you have (even if it works) and I'll be happy to comment on your code.
Avatar of J_B

ASKER

Another question - when searching for ,"  (a comma followed by a double quote)  - do you know the syntax for including the double quote inside " "?

I tried ",""" (Ithought adding 2 double quotes would work but it just returning the position of the commas.
Avatar of J_B

ASKER

Got the answer to my question above - just use the \ (escape character)

I am getting closer.
Avatar of J_B

ASKER

phoffric: - Thank you very much!!!!! I am not completely done yet but I am confident I will be able to parse the final field.

I got the first two fields parsed successfully using the below code:


size_t found;
size_t found2;
found = entireLine.find_first_of(",");
field1 = entireLine.substr (0,found); 

found2 = entireLine.find_first_of(",",found+1);
field2  = entireLine.substr (found+1,found2 - (found + 1)); 

Open in new window

Avatar of J_B

ASKER

phoffric:  - Thanks again!!!
I got the final field using the below code
(FYI - after looking at the files not all of the field3s had quotes around them. so just needed to use the first two commas.)

  field3 = entireLine.substr (found2+1,entireLine.length());

Open in new window

Avatar of J_B

ASKER

Thanks! for all your help!!!
Back again, and I see you did a good job!
All looks good.
In your OP, I see that after the comma is a space. And your parsed out strings can begin with a space. Just wanted to see if that is OK with you. From you questions and answers, I know that if it's an issue, you'll have no problem dealing with it.
Avatar of J_B

ASKER

The space was just my mistake in typing it. The file doesn't have any spaces.
Thanks again.