Stringstreams Getline

J

JackC

Hi,

How do i use stringstreams getline function to extract lines from an
existing string?

Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n";

I want to extract each line out into a vector array of strings, but i
cant get stringstream working :(

This is the sort of code i have tried:

// istream& getline (char* s, streamsize n )

stringstream MyStream;
MyStream<<strlist;
string extracted_line = MyStream.getline(mystring, 0);

Clearly thats very wrong, but im not sure how to correctly use
getline()? Also how would i loop this up so it extracts all the lines
in the string? Would my condtion just be checking extracted_line !=
std::string::npos?

Thanks for any help,
Jack
 
R

red floyd

JackC said:
Hi,

How do i use stringstreams getline function to extract lines from an
existing string?

Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n";

I want to extract each line out into a vector array of strings, but i
cant get stringstream working :(

This is the sort of code i have tried:

// istream& getline (char* s, streamsize n )

stringstream MyStream;
MyStream<<strlist;
string extracted_line = MyStream.getline(mystring, 0);
#include <string>
#include <sstream>
#include <istream>
#include <vector>

int main()
{
std::string strlist("line1\r\nLine2\r\nLine3\r\n");
std::istringstream MyStream(strlist);
std::vector<std::string> v;
std::string s;
while (std::getline(MyStream, s))
v.push_back(s);
return 0;
}


Clearly thats very wrong, but im not sure how to correctly use
getline()? Also how would i loop this up so it extracts all the lines
in the string? Would my condtion just be checking extracted_line !=
std::string::npos?

Thanks for any help,
Jack
 
B

BobR

#include <string>
#include <sstream>
#include <istream>
#include <vector>

int main(){
std::string strlist("line1\r\nLine2\r\nLine3\r\n");
std::istringstream MyStream(strlist);
std::vector<std::string> v;

// > std::string s;
// > while (std::getline(MyStream, s))
// > v.push_back(s);

// ref: A. Koenig/W. Brown
for( std::string line; std::getline( MyStream, line ); ){
v.push_back(line);
} // for(line)
return 0;
}

Why? Because you can! <G> Hi red.

To OP (from a post by Gavin Dean):
std::getline allows you to choose any delimiter character you want. The
default is newline but you can change that. This shows how it works.

#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
string input("abc\tdef\nghi");
istringstream iss(input);
string read_to_newline;
getline(iss, read_to_newline);
iss.str(input);
string read_to_tab;
getline(iss, read_to_tab, '\t');

cout << read_to_newline << "\n";
cout << read_to_tab << "\n";
}
 
R

red floyd

BobR said:
// > std::string s;
// > while (std::getline(MyStream, s))
// > v.push_back(s);

// ref: A. Koenig/W. Brown
for( std::string line; std::getline( MyStream, line ); ){
v.push_back(line);
} // for(line)


Why? Because you can! <G> Hi red.

Hi Bob. Actually, I had considered that construct, but used the while
loop construct, based on my estimate of OP's skills.
[redacted]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top