evaluating a string, part 2

hello,

ok so im having some success with my string manipulation, but i still have a problem.

this is what i have so far:

// Start
        ifstream fInputVec(stVecFile.c_str(), std::ios::in);

        string stFileLine;

      while(getline(fInputVec, stFileLine))
      {
            string stFileWord;
            stringstream ssFileLine(stFileLine);

            while (ssFileLine.good())
            {
                  ssFileLine >> stFileWord;

                  // If string is a blank space, skip to next >>
                  if (stFileWord.length() == 0)
                        continue;
                  
                       // PROBLEM IS HERE
                  if (stFileWord.substr(1, 1) == "<")
                        stFileWord = stFileWord.substr(2, stFileWord.length() - 1);
                  

                  cout << stFileWord << endl;
            }

      }

// End

ok so the problem im having is that in my stream i will have the two '<' '>' characters either at the beginning of the stream or at the end. i need to basically delete them from my stream.

the above code was an attempt to resize my string if the '<' char was at the start of it. it doesnt work. for some reason the program just doesnt realise that '<' is the first letter of the string.

does anyone have any working and more efficient solutions?

thanks

gem56Asked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
>>                   if (stFileWord.substr(1, 1) == "<")

Indexing in strings is 0-based : the first character has index 0, not index 1. So, make that :

                  if (stFileWord.substr(0, 1) == "<")

Faster would be :

                  if (stFileWord[0] == '<')

0
 
gem56Author Commented:
champion, thanks again.
0
 
gem56Author Commented:
sorry to keep breaking b@lls mate, i've updated the code (i had an error with stFileWord[0] saying it couldnt convert between char* and int, so i used the substr from 0 index instead, works fine), ok so now im faced with another problem. the chars '<' and '>' have to be removed from my stream, so instead of doing it after i've sent my stream into a word (ssFileLine >> stFileWord;) and then checking each word, i figured it would be better to remove all instances of '<' and '>' after my getline (makes it easy and less clattered when i have to check if the length > 0)

i've looked at the strings .replace fuction, however it doesnt automatically search the string and replace what you want (like in VB :( ), the understanding i got was that you had to give it a position in the string at which to replace with your new character. would this be the easiest function/method to use to remove these two chars from my string?

.....also, how would i go about doing it?

thanks again
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Infinity08Commented:
>> (i had an error with stFileWord[0] saying it couldnt convert between char* and int, so i used the substr from 0 index instead, works fine)

Did you use the exact code I posted :

                  if (stFileWord[0] == '<')

Note the single quotes instead of the double quotes !


>> .....also, how would i go about doing it?

You can use a combination of find and erase :

        http://www.cplusplus.com/reference/string/string/find.html
        http://www.cplusplus.com/reference/string/string/erase.html

ie. you find the character you want to remove, and then erase it.


Can you give an example of a line from the file, and explain which tokens you want to get out of it ? So that I have an idea of what you want to do exactly ?
0
 
gem56Author Commented:
yeah my mistake with the quotes.

ok i havnt checked out the links you posted yet, however i've created something using those functions anyway (originally started with a find/replace loop)

Heres what i have:

// Start

      while(getline(fInputVec, stFileLine))
      {

//------------------------------------------------------------

            // Remove any '<' and '>' chars from string
            while (stFileLine.length() > stFileLine.find("<"))
            {
                  int iStart = stFileLine.find("<");
                  stFileLine.erase(iStart, 1);
            }

            while (stFileLine.length() > stFileLine.find(">"))
            {
                  int iStart = stFileLine.find(">");
                  stFileLine.erase(iStart, 1);
            }

//------------------------------------------------------------

            string stFileWord;
            stringstream ssFileLine(stFileLine);

                // While theres text still left in the stream....
            while (ssFileLine.good())                                       // Is there something better then '.good()' ?
            {
                  ssFileLine >> stFileWord;

                  // If string is a blank space, skip to next >>
                  if (stFileWord.length() == 0)
                        continue;
            
                  cout << stFileWord << endl;
            }

      }

// End


ok typically the file im trying to open looks like this:

//-----------------------------------------
image   BMP     300   300

colour < 255 50 0  >
draw Point <50 145   >
draw Point < 50 155>

colour <255 52 0>
draw Point <52 145>
draw Point <52 155>
//-----------------------------------------

.....without the //----- lines. so far the code i've written works fine, except for the first few lines where i have spaces between the last digit and the '>' char. in these cases the output seems to repeat the last digit as well. im not sure as to why though.

the output im getting is something like:
image
BMP
300
300
colour
255
50
0
0                 // <----- Repeated output shouldnt be there
draw
Point
50
145
145             // <----- Repeated output shouldnt be there
...etc

appreciate all your help :)
0
 
Infinity08Connect With a Mentor Commented:
Instead of your loop :

            while (stFileLine.length() > stFileLine.find("<"))
            {
                  int iStart = stFileLine.find("<");
                  stFileLine.erase(iStart, 1);
            }

you can use :

            size_t iStart = 0;
            while ((iStart = stFileLine.find("<", iStart)) != string::npos) {
                  stFileLine.erase(iStart, 1);
            }


>>             while (ssFileLine.good())                                       // Is there something better then '.good()' ?

You can use :

        while (ssFileLine >> stFileWord)


>> in these cases the output seems to repeat the last digit as well. im not sure as to why though.

Because your code thinks there is still a word at the end of the line (because of the use of good()).

Btw, to skip whitespace, you can use :

        ssFileLine >> ws;

You can of course also combine that with actually readin the value :

        ssFileLine >> ws >> stFileWord;
0
 
gem56Author Commented:
excellent, made the changes and everything works perfect now.

thanks heaps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.