LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to split string into an array/list of lines in C++? (http://www.linuxquestions.org/questions/programming-9/how-to-split-string-into-an-array-list-of-lines-in-c-812223/)

rohedin 06-04-2010 06:24 PM

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.
This is my second line.
This is my third line.

into,
Code:

"This is my first line.", "This is my second line.", "This is my third line."
in C++.

i.e. split the code at every new line :)


Thanx in advance!
-Ben

Sergei Steshenko 06-04-2010 06:34 PM

Quote:

Originally Posted by rohedin (Post 3992877)
How can i split a string like this,
Code:

This is my first line.
This is my second line.
This is my third line.

into,
Code:

"This is my first line.", "This is my second line.", "This is my third line."
in C++.

i.e. split the code at every new line :)


Thanx in advance!
-Ben

Read

man fgets

and find its C++ counterpart, though it's not strictly necessary.

tuxdev 06-04-2010 07:03 PM

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");
std::string line;
while(std::getline(file, line)) {
  // whatever
}


rohedin 06-05-2010 05:18 AM

Thanx :)
 
Quote:

Originally Posted by tuxdev
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");
std::string line;
while(std::getline(file, line)) {
  // whatever
}


Does std::getline() only work on files? Can it be used on strings?
e.g.
Code:

std::string text = " one \n two \n three";
std::string line;
while(std::getline(text, line)) {
  // whatever
}

If not... no big deal :) would be nice tho :D

tuxdev 06-05-2010 10:00 AM

Use stringstreams:
Code:

std::istringstream stream(text);
std::string line;
while(std::getline(stream, line)) {
  // whatever
}


rohedin 06-06-2010 03:18 AM

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);
std::string line;
while(std::getline(stream, line)) {
  array.append(line); //Or how ever else you would go
                      //about appending to a list.
}

Thanx :D

harry edwards 06-06-2010 03:30 AM

I would personally use string tokenizing in c This method will eradicate the need for an array.

posixculprit 06-06-2010 03:41 AM

http://www.cplusplus.com/reference/stl/

Harry Edwards: Care to prove that strtok() eradicates one's need for an array?

harry edwards 06-06-2010 04:24 AM

The example in the link does exactly that:

Code:

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?

posixculprit 06-06-2010 04:48 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,",");
}


johnsfine 06-06-2010 06:24 AM

Quote:

Originally Posted by rohedin (Post 3994043)
Is there any way of putting those lines into an array without predefining the arrays length. The array must be changeable in size.

Use std::vector instead of an array.

rohedin 06-06-2010 10:54 AM

Soz :)
 
Quote:

Originally Posted by posixculprit (Post 3994082)
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 :p


All times are GMT -5. The time now is 03:20 AM.