![]() |
how to split string into an array/list of lines in C++?
How can i split a string like this,
Code:
This is my first line.Code:
"This is my first line.", "This is my second line.", "This is my third line."i.e. split the code at every new line :) Thanx in advance! -Ben |
Quote:
man fgets and find its C++ counterpart, though it's not strictly necessary. |
The (superior) C++ counterpart to fgets() is std::getline(std::istream &, std::string &). Unlike fgets(), std::getline will properly handle lines of any length for free.
Code:
std::ifstream file("filename"); |
Thanx :)
Quote:
e.g. Code:
std::string text = " one \n two \n three"; |
Use stringstreams:
Code:
std::istringstream stream(text); |
Add lines into array.
Is there any way of putting those lines into an array without predefining the arrays length. The array must be changeable in size.
e.g. Code:
std::istringstream stream(text); |
I would personally use string tokenizing in c This method will eradicate the need for an array.
|
http://www.cplusplus.com/reference/stl/
Harry Edwards: Care to prove that strtok() eradicates one's need for an array? |
The example in the link does exactly that:
Code:
char *tok = strtok(szString, ","); |
Unless I have missed something, the OP has not mentioned what he intends to do with the extracted "lines". We are only informed that he is looking for a method to obtain them. Assume his "actual" problem is to sort the lines in alphanumeric order, the following approach no longer fits, does it:
Code:
char *tok = strtok(szString, ","); |
Quote:
|
Soz :)
Quote:
What I want to do is insert a character at point x, y in the string. x being the number of characters into the line and y being the line the character is inserted into. ;) If you know a function that could do that for me it would be very helpful but I would learn a lot less :p |
| All times are GMT -5. The time now is 03:41 AM. |