944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4346
  • C++ RSS
Oct 21st, 2009
0

C++ String Parsing?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string document;
  9. string temp;
  10. ifstream infile;
  11.  
  12. infile.open("file.txt");
  13.  
  14.  
  15. while (! infile.eof())
  16. {
  17. infile >> temp; // Put contents of infile (file.txt) into temp. Temp is here b/c it will be used later at some point....
  18. document += temp; // Put contents of temp into document.
  19. }
  20.  
  21. cout << document << endl; // read document to varify string is correct
  22. for(int i=0; i<document.length(); i++) {
  23. 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.
  24. };
  25. }
  26. infile.close();
  27. system("pause");
  28. return 0;
  29. }

I currently have a text document (file.txt) that looks like this:
C++ Syntax (Toggle Plain Text)
  1. <name first>joe</name>
  2. <name first>ann</name>
  3. <name>bob builder</name>

and the program spits out this:
C++ Syntax (Toggle Plain Text)
  1. <namefirst>joe</name><namefirst>ann</name><name>bobbuilder</name>
  2.  
  3. '<' encountered
  4. '<' encountered
  5. '<' encountered
  6. some more of them in here.... as many of them as there are characters in the file.
  7. '<' encountered
  8. '<' encountered
  9. '<' encountered
  10. Press any key to continue . . .
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 if (document[i] = '<') into if (document[i] = "<") , i get a invalid conversion from `const char*' to `char' error.
Similar Threads
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Oct 21st, 2009
0
Re: C++ String Parsing?
By using 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)
  1. while (getline (infile, temp))
  2. { 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.
Reputation Points: 18
Solved Threads: 10
Junior Poster
DemonGal711 is offline Offline
190 posts
since Apr 2008
Oct 21st, 2009
0
Re: C++ String Parsing?
Aww man wow i fell stupid right now... I ALWAYS remember the entire = vs == difference, i guess I was so caught up in the programming though...

Hey thanks a lot for the quick reply dude!
Reputation Points: 10
Solved Threads: 3
Junior Poster in Training
dylank is offline Offline
66 posts
since Oct 2009
Oct 21st, 2009
0
Re: C++ String Parsing?
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.

C++ Syntax (Toggle Plain Text)
  1. int index = 0;
  2. string sentence = "My IQ is < than schfifty-five";
  3.  
  4. index = sentence.find('<');
  5.  
  6. 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)
  1. string sentence = "More than a feeling.";
  2. string sub;
  3.  
  4. sub = sentence.substr(6,4);
  5.  
  6. 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)
  1. string sentence = "We should just be friends";
  2.  
  3. for(int i=0; i<sentence.size(); i++)
  4. {
  5. //now you will never run out of bounds on your <string> class objects teehee
  6. }
Last edited by Clinton Portis; Oct 21st, 2009 at 10:44 pm.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Repeated output and Problem Skipping names?
Next Thread in C++ Forum Timeline: Please Help Me





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC