evaluating a string

Posted on 2007-10-19
Last Modified: 2010-04-01
hey guys,

i have a function that opens a text file and reads off one line at a time. the line of text is stored in a string.

an example of a line within the file is:

paint box 4 <110 110> <110 190> <190 110> <110 110>

however the problem is that not all values/words will be separated by one white space, there can be an unknown number of them at once.

the aim is to verify the first two words, then stick the rest of the numbers into variables.

whats the easiest way i can do this?

Question by:gem56
    LVL 53

    Expert Comment

    Is this a different question than this one :


    ? If not, can you say which part you're still having trouble with ?

    Author Comment

    yeah sorry champ, its the same sort of question, but with those solutions im having too many problems trying to make it work with what i need. mainly the solutions are too complex and the output isnt helping me too much.

    im just looking for something simple (simpler) that i can use to evaluate the string sequentially.


    Author Comment

    LVL 53

    Accepted Solution

    Did you read the tutorial on file intput/output that I posted in the other post :


    That's quite basic C++, and is something that you really have to understand if you want to get into C++. So, let me know if there's something that is unclear there. Especially take a look at the code samples in that tutorial.

    >> im just looking for something simple (simpler) that i can use to evaluate the string sequentially.

    It can't get much simpler than what I suggested :

            1) read a line from the file into a string (using getline)
            2) read each token (word) from the line one at a time, using stream operations. For example,

                      string line;        // <--- filled up from the file by getline
                      stringstream ss(line);               // <--- create a stringstream for the line

                      string word;
                      ss >> word;           // <--- read the first word from the line and store it in word
                      // <--- do something with the word

                      ss >> word;           // <--- read the second word from the line and store it in word
                      // <--- do something with the word

                      etc. ...

            3) go back to 1) for the next line

    Does that make it clearer ? I would suggest to just read the tutorial and on-line documents I posted earlier, and then just play with it a bit to see if you can get things to work. Start with basic things, like reading a line from the file, and printing it, and then start expanding the code to do more complex things ...

    Author Comment


    looks like i'll have to find a tutorial that explains stringstream's better (the one i have has about 3 sentences on it).

    thanks a lot mate, exactly what i needed :)

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now