I'm tinkering with C++ (I have a COBOL, Perl and PHP background) and I'm having trouble splitting a string so that I can act on each parsed field. I believe the trouble is to do with my lack of understanding of how C++ handles variables.
I've slurped a file's contents into an array successfully and I can act on each complete line (e.g. search/replace text). I also found some great examples of how to parse strings. The problem occurs when I try to apply the parse examples to the strings in my array.
The strings are created via:
std::vector<std::string> File_Lines;
ifstream file(filename.c_str());
while ( file.good() ) {
getline ( file, line, '\n' );
string InputLine = string( line );
File_Lines.push_back(line);
}
One working example of parsing strings (that I found on this site) is:
char myString[] = "The quick brown fox";
char *p = strtok(myString, " ");
while (p) {
printf ("Token: %s\n", p);
p = strtok(NULL, " ");
}
The problem starts when I try to feed my string to the code that does the parsing. Feeding it directly:
char myString[] = File_Lines[array_counter];
gives "error: initializer fails to determine size of ‘myString’"
If I try converting using "std::string" (as suggested in other answers on this site):
std::string File_Line;
File_Line = File_Lines[array_counter];
char myString[] = File_Line;
...I get the same error.
Other answers suggested adjusting the code to:
char *myString = File_Line;
but that just gives me "error: cannot convert ‘std::string {aka std::basic_string}’ to ‘char*’ in initialization"
I'm aware that the problem is due to my own ignorance, but I'd really appreciate any help on how to feed a string into a parser.
Also, if anyone has any simple explanation of how to convert between the data types, that would be great.