[SOLVED] how to split string into an array/list of lines in C++?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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);
std::string line;
while(std::getline(stream, line)) {
array.append(line); //Or how ever else you would go
//about appending to a list.
}
char *tok = strtok(szString, ",");
while (tok != NULL) {
// Do something with the tok
tok = strtok(NULL,",");
}
I've used this method several times. 'tok' holds the string which represents the current token, subsequent calls to strtok moves the tok pointer onto the next token. To me that's eradicating the need for an array, though, I may have misunderstood?
Last edited by harry edwards; 06-06-2010 at 04:25 AM.
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, ",");
while (tok != NULL) {
// Do something with the tok
tok = strtok(NULL,",");
}
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, ",");
while (tok != NULL) {
// Do something with the tok
tok = strtok(NULL,",");
}
Sorry... You have a valid point there.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.