LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-04-2010, 06:24 PM   #1
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Rep: Reputation: 15
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
 
Old 06-04-2010, 06:34 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by rohedin View Post
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.
 
0 members found this post helpful.
Old 06-04-2010, 07:03 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
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
}
 
1 members found this post helpful.
Old 06-05-2010, 05:18 AM   #4
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
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
 
Old 06-05-2010, 10:00 AM   #5
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
Use stringstreams:
Code:
std::istringstream stream(text);
std::string line;
while(std::getline(stream, line)) {
   // whatever
}
 
1 members found this post helpful.
Old 06-06-2010, 03:18 AM   #6
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Question 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
 
Old 06-06-2010, 03:30 AM   #7
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

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

Last edited by harry edwards; 06-06-2010 at 03:31 AM.
 
Old 06-06-2010, 03:41 AM   #8
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
http://www.cplusplus.com/reference/stl/

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

Last edited by posixculprit; 06-06-2010 at 03:46 AM.
 
Old 06-06-2010, 04:24 AM   #9
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 47
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?

Last edited by harry edwards; 06-06-2010 at 04:25 AM.
 
Old 06-06-2010, 04:48 AM   #10
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
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,",");
}
 
0 members found this post helpful.
Old 06-06-2010, 06:24 AM   #11
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Centos
Posts: 4,969

Rep: Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075
Quote:
Originally Posted by rohedin View Post
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.
 
1 members found this post helpful.
Old 06-06-2010, 10:54 AM   #12
rohedin
LQ Newbie
 
Registered: May 2010
Distribution: Linux Mint 8 - Gnome
Posts: 22

Original Poster
Rep: Reputation: 15
Soz :)

Quote:
Originally Posted by posixculprit View Post
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
 
  


Reply

Tags
array, c++, list, split, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting: split file into 12 lines array zklone Programming 12 12-07-2009 10:02 PM
How to split a string into a linked list? (C++ question) cabeca Programming 3 11-28-2009 09:09 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration