| | |
String Parsing using known delimiters? [C++]
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 35
Reputation:
Solved Threads: 0
The age old issue of string parsing comes up again ...
I have a text file that contains lines that are SUPPOSED to follow a set format, specifically:
string, string, long string int string double int
The delimiters are therefore:
Comma (,) for the first two fields
Spaces for all other fields
Strings like this would be valid:
Jon, Jack, 100 CPN 5 KTE 1.00 10
Jon, Jack 100 CPN 5 KTE 1.00 10 // notice the extra spaces
Whereas something like these would be considered invalid:
Jon Jack 100 CPN 5 KTE 1.00 10 // missing the commas
Jon, Jack, 100 CPN 5 KTE 1.00 // missing the last field "10"
Jon, Jack, 100CPN 5 KTE 1.00 10 // missing space between "100" and "CPN"
The goal is to EXTRACT each section and store them, and if possible determine when a string is INVALID (does not follow format).
I have a class with the following data members:
So, how can I parse the string (sLine) and extract each piece into there components (A, B, C, D, E, F, G, H)...
I was thinking of using the old method of simply doing substring searches but I find it very error prone and long ... is there a better way to accomplish this?
Anything anyone would recommend?
Any help would be much appreciated...
Thanks,
I have a text file that contains lines that are SUPPOSED to follow a set format, specifically:
string, string, long string int string double int
The delimiters are therefore:
Comma (,) for the first two fields
Spaces for all other fields
Strings like this would be valid:
Jon, Jack, 100 CPN 5 KTE 1.00 10
Jon, Jack 100 CPN 5 KTE 1.00 10 // notice the extra spaces
Whereas something like these would be considered invalid:
Jon Jack 100 CPN 5 KTE 1.00 10 // missing the commas
Jon, Jack, 100 CPN 5 KTE 1.00 // missing the last field "10"
Jon, Jack, 100CPN 5 KTE 1.00 10 // missing space between "100" and "CPN"
The goal is to EXTRACT each section and store them, and if possible determine when a string is INVALID (does not follow format).
I have a class with the following data members:
C++ Syntax (Toggle Plain Text)
class A { private: // Record string A string B long C; string D; string E; string F; double G; int H; public: A(string sLine); // constructor }; A::A(string sLine) { // somehow parse the string here and determine if it is valid // }
So, how can I parse the string (sLine) and extract each piece into there components (A, B, C, D, E, F, G, H)...
I was thinking of using the old method of simply doing substring searches but I find it very error prone and long ... is there a better way to accomplish this?
Anything anyone would recommend?
Any help would be much appreciated...
Thanks,
•
•
Join Date: Dec 2007
Posts: 360
Reputation:
Solved Threads: 69
You can find many solutions for this on codeproject.com, i.e. http://www.codeproject.com/KB/recipes/Tokenizer.aspx or http://www.codeproject.com/KB/recipes/stringtok.aspx
Last edited by jencas; Jul 8th, 2009 at 9:17 am. Reason: added another link
If you are forced to reinvent the wheel at least try to invent a better one!
Please use code tags - Please mark solved threads as solved
Please use code tags - Please mark solved threads as solved
![]() |
Similar Threads
- string parsing (C#)
- String parsing (C++)
- repost: leak using c++ string (C++)
- leak using c++ string (C++)
Other Threads in the C++ Forum
- Previous Thread: How to create a static map that is only loaded once? [C++]
- Next Thread: Questions on vector
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







