Given a string say string joe
lets say joe contains
-----------
Hey
20403
How are you doing today?
Thats nice have a nice day.
Don't forget to brush your teeth.
----------
How do I get line by line the contents of joe. and how would I convert some of those lines... say the numbers into different types like ints
thanks
Basic C++ question (strings)?
Um, are those 5 lines all part of only one string?
If they were seperate, it would be a bit easier to process them. Anyway....
If you want to convert an ASCII number into an integer, you can use a function for that called, (I think) atoi().
If all 5 lines are one string, then you have to use a loop to search through the string.
When you come across an ASCII digit, then you copy the character to an array (make sure you allocated one with enough space), and then continue until you come across a 'space' character. Make sure you insert a zero at the end of the last copied ASCII digit because C/C++ strings should be zero-terminated.
Next, you just pass the array to the atoi() function.
Reply:That's more than one question. But, first of all, if you parse joe character by character you will necessarily find line feed characters. They (and the associated carriage returns) signify the beginning of a new field.
jasmine
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment