C++ String Parsing?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster

C++ String Parsing?

 
0
  #1
Oct 21st, 2009
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:

  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:
  1. <name first>joe</name>
  2. <name first>ann</name>
  3. <name>bob builder</name>

and the program spits out this:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 185
Reputation: DemonGal711 is an unknown quantity at this point 
Solved Threads: 10
DemonGal711 DemonGal711 is offline Offline
Junior Poster
 
0
  #2
Oct 21st, 2009
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: dylank is an unknown quantity at this point 
Solved Threads: 0
dylank dylank is offline Offline
Newbie Poster
 
0
  #3
Oct 21st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 481
Reputation: Clinton Portis is on a distinguished road 
Solved Threads: 58
Clinton Portis's Avatar
Clinton Portis Clinton Portis is offline Offline
Posting Pro in Training
 
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.

  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Reply

Tags
c++, error, file

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 879 | Replies: 3
Thread Tools Search this Thread



Tag cloud for c++, error, file
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC