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:

#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:

<name first>joe</name>
<name first>ann</name>
<name>bob builder</name>

and the program spits out this:

<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 . . .

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.

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.

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 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.

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!

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.

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:

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:

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
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.