Is there any function to read one line from a string?I know its a little stupid, I searched MSDN.Code:#include <string>
std::string str, str2;
std::getline(cin, str, 'p');
//Now I want read one line of it and store it in str2, how?
Thanks
Printable View
Is there any function to read one line from a string?I know its a little stupid, I searched MSDN.Code:#include <string>
std::string str, str2;
std::getline(cin, str, 'p');
//Now I want read one line of it and store it in str2, how?
Thanks
There's only one line in str, so I guess it would be.Quote:
//Now I want read one line of it and store it in str2, how?
Code:str2 = str;
Wow, sorryyyyyyyyy. I am going to edit it.
You can either read until you find a '\n', or you can read it as if it was a stream with <stringstream>
Maybe you can combine substr and find. This looks like it works.
Code:#include <iostream>
#include <string>
using std::cout;
using std::string;
int main()
{
string str = "Hi there!\nI'm happy!";
string str2 = str.substr(0, str.find('\n'));
cout << "First line = " << str2 << "\n";
cout << "Second line = " << str.substr(str.find('\n') + 1) << "\n";
return 0;
}
I know. I usually don't work with string and less than it string stream. I am serching for a function to read a line. That seems impossible for string. So I am going to use string stream.Quote:
Maybe you can combine substr and find.
How can I read from cin to string stream? I think I knew these things some day, but I can't remember.
[edit]
I read them somewhere didn't learn them really.
I know how to do that! I was reading about it just yesterday and thought it was really neat. I don't really know how it works though. :(Quote:
Originally Posted by siavoshkc
Code:#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::stringstream stream;
std::string str2;
stream << std::cin.rdbuf();
std::getline(stream, str2);
std::cout << str2 << "\n";
return 0;
}
No problem I know. Reads input buffer and puts it into stream then reads one line from stream and puts it into str2.Quote:
I don't really know how it works though.
getline works with stringstreams the same way it does with cin. This is so because the first parameter is a reference to an input stream.
Code:#include <sstream>
#include <iostream>
#include <string>
int main() {
std::istringstream istr;
std::string str;
std::getline(istr, str, 'p');
}
rdbuf() don't work because there is no delim and nothing will come to string from keyboard.
http://www.cplusplus.com/ref/iostream/ios/rdbuf.html
That explains how to use rdbuf(). You first need to associate your stream with the correct buffer
It gets cin streambuf and puts it into stream. But this code won't work as we want because there is no delim for cin to put characters into streambuf. And because it never reaches EOF, it will get keys from keyboard forever.Code:stream << std::cin.rdbuf();
std::getline(stream, str2);
std::cout << str2 << "\n";
I faked EOF by typing ctrl+z on my Windows computer, but I don't know how to get it to work any other way. Sorry. :( I think if you want to stop at something like 'p' from cin, you need to read a string from cin, then use the string to make a stringstream, then read from the stringstream with a different delim.Quote:
Originally Posted by siavoshkc
Is that what you wanted?Code:#include <iostream>
#include <sstream>
#include <string>
using std::string;
using std::getline;
int main()
{
string str;
string str2;
getline(std::cin, str, 'p');
std::stringstream stream(str);
getline(stream, str2);
std::cout << str2 << "\n";
return 0;
}
I used ctrl+z but it works crazy. It goes crazy after pressing a key. It is cin.get() that losses its functionality after reading rdbuf().
Code:stringstream inp;
inp << cin.rdbuf();
cin.get();
Ah, isn't there a flag in cin that's set when it sees EOF? This is just a guess, but I bet it would work if you cleared it. :) I had that problem with a guessing game I wrote last week, and using cin.clear() fixed it all up.Quote:
Originally Posted by siavoshkc