Question : 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.






Answer : C++ Parsing a string from an input file

Good. Now strcspn() is very nice for C code. To move towards C++, take a look at
           string::find_first_of
    http://www.cplusplus.com/reference/string/string/find_first_of/
The code below is from the example in this link.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
// string::find_first_of
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("Replace the vowels in this sentence by asterisks.");
  size_t found;

  found=str.find_first_of("aeiou");
  while (found!=string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  cout << str << endl;

  return 0;
}
Random Solutions  
 
programming4us programming4us