| | |
C++ String Parsing?
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
Hi, I was hoping someone could help me with a C++ string problem. I currently have my program open up a file, read the contents into a string, and then I am trying to look for certain key characters in the string, like < and </. In short, I am trying to read an XML type file.
Here is my code so far:
I currently have a text document (file.txt) that looks like this:
and the program spits out this:
The program also strips out white spaces. I guess there not really necessary, but if anyone knows how to preserve them that would be nice.
By the way, when I make
Here is my code so far:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { string document; string temp; ifstream infile; infile.open("file.txt"); while (! infile.eof()) { infile >> temp; // Put contents of infile (file.txt) into temp. Temp is here b/c it will be used later at some point.... document += temp; // Put contents of temp into document. } cout << document << endl; // read document to varify string is correct for(int i=0; i<document.length(); i++) { if (document[i] = '<') { cout << endl << "'<' encountered" << endl; //Go to a function that takes care of the rest of the file. I will pass the string and 'i' to it. }; } infile.close(); system("pause"); return 0; }
I currently have a text document (file.txt) that looks like this:
C++ Syntax (Toggle Plain Text)
<name first>joe</name> <name first>ann</name> <name>bob builder</name>
and the program spits out this:
C++ Syntax (Toggle Plain Text)
<namefirst>joe</name><namefirst>ann</name><name>bobbuilder</name> '<' encountered '<' encountered '<' encountered some more of them in here.... as many of them as there are characters in the file. '<' encountered '<' encountered '<' encountered Press any key to continue . . .
By the way, when I make
if (document[i] = '<') into if (document[i] = "<") , i get a invalid conversion from `const char*' to `char' error. •
•
Join Date: Apr 2008
Posts: 185
Reputation:
Solved Threads: 10
0
#2 Oct 21st, 2009
By using
The reason you get the error is because document[i] is a character and "<" is a string literal and literals are constant.
What exactly are you trying to accomplish with this code, that will help in determining the best way to write it.
infile >> temp; you are striping the whitespace. A work around is document += temp + ' '; but you will lose the returns. If you want to preserve the returns, I would suggest the below code, but basically all that does is copy each line into document. C++ Syntax (Toggle Plain Text)
while (getline (infile, temp)) { document += temp + '\n'; }
if (document[i] = '<') means you are setting every letter in document to that character. Is that what you want? If you are looking for the '<' in the text, you would want this instead if (document[i] == '<') The reason you get the error is because document[i] is a character and "<" is a string literal and literals are constant.
What exactly are you trying to accomplish with this code, that will help in determining the best way to write it.
Last edited by DemonGal711; Oct 21st, 2009 at 10:22 pm.
0
#4 Oct 21st, 2009
Now that you are using <string> class objects, it is time to unlock the magic of the almighty string.
Here is a list of string object members:
http://www.cplusplus.com/reference/string/string/
On of them that you may find of particular interest is the find( ) function, which will return the element position of a character, or the first position of a word.
Another member ye' may find of interest is substr( ) which gives you the ability to extract a piece of a string:
One more member of interest, since you are performing array operations on your strings, be sure not to run out of bounds of the array.. that's where size( ) comes in:
Here is a list of string object members:
http://www.cplusplus.com/reference/string/string/
On of them that you may find of particular interest is the find( ) function, which will return the element position of a character, or the first position of a word.
C++ Syntax (Toggle Plain Text)
int index = 0; string sentence = "My IQ is < than schfifty-five"; index = sentence.find('<'); cout << "The element position of < is located at sentence[" << index << "]";
Another member ye' may find of interest is substr( ) which gives you the ability to extract a piece of a string:
C++ Syntax (Toggle Plain Text)
string sentence = "More than a feeling."; string sub; sub = sentence.substr(6,4); cout << "More " << sub << " a feeling.";
One more member of interest, since you are performing array operations on your strings, be sure not to run out of bounds of the array.. that's where size( ) comes in:
C++ Syntax (Toggle Plain Text)
string sentence = "We should just be friends"; for(int i=0; i<sentence.size(); i++) { //now you will never run out of bounds on your <string> class objects teehee }
Last edited by Clinton Portis; Oct 21st, 2009 at 10:44 pm.
![]() |
Similar Threads
- String Parsing using known delimiters? [C++] (C++)
- String parsing (C++)
- Code Snippet: Parsing a string: By lines. (C)
- repost: leak using c++ string (C++)
- leak using c++ string (C++)
Other Threads in the C++ Forum
- Previous Thread: Repeated output and Problem Skipping names?
- Next Thread: Please Help Me
Views: 879 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for c++, error, file
3d ada api application array arrays assignment associations background binary bitmap blue bmp c# c++ char char* class code compile compiler compression console csv data database desktop development download email embed encryption error file format fstream function functions game gmail graph gui ifstream input insert int java jni line lines linker linux math microsoft music mysql newbie open output parameter parse php pointer print problem program programming projects python read reading recursion recursive reference rpg savefile screen search server stream string strings struct syntax system template templates text tree update upload url variable vb6 vista visual void win32 windows write






