Reply
Thread Tools
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#1
Hi
Is it possible to read all the words from a line in c++?
I mean, I got a line

Code:
blacklist = mediaplayer Conky
but it also can be
Code:
blacklist = mediaplayer Conky osso-xterm pygtkeditor
so the items number can change randomly\

Is it possible to read 'em all no matter how many they are?
 
peter2p's Avatar
Posts: 254 | Thanked: 146 times | Joined on Dec 2010 @ Antwerp Belgium
#2
Originally Posted by marmistrz View Post
Hi
Is it possible to read all the words from a line in c++?
I mean, I got a line

Code:
blacklist = mediaplayer Conky
but it also can be
Code:
blacklist = mediaplayer Conky osso-xterm pygtkeditor
so the items number can change randomly\

Is it possible to read 'em all no matter how many they are?

I found this in my transitions.ini
# Lock application window in landscape mode.
# Example: blacklist = mediaplayer osso-xterm worldclock image-viewer camera-ui
blacklist=blessn900 ati85 Conky clean900
so far so good, they all stay in landscape mode
__________________
.................................................. ...........
..........................
................................N900 is a way of life....................
surfing the web, navigate, chat, ttweet, email, scan, hack, share, tweak...
.....................and you can also use him to make a call
 
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#3
Yes. this is what I'm talking about

I wanna read this with getline and have from

Code:
blacklist = Conky mediaplayer
Code:
blacklist
Code:
Conky
Code:
mediaplayer
as std::string each

Do you understand?
 
peter2p's Avatar
Posts: 254 | Thanked: 146 times | Joined on Dec 2010 @ Antwerp Belgium
#4
Originally Posted by marmistrz View Post
Yes. this is what I'm talking about

I wanna read this with getline and have from
as std::string each

Do you understand?
No, sorry, this is for using C++ i guess, don't know anything about that, i just followed this thread by Boemien and added the names of the apps on the transitions file, works great
__________________
.................................................. ...........
..........................
................................N900 is a way of life....................
surfing the web, navigate, chat, ttweet, email, scan, hack, share, tweak...
.....................and you can also use him to make a call
 
Posts: 3,328 | Thanked: 4,476 times | Joined on May 2011 @ Poland
#5
Yep, this is for using C++
How can I read all the stuff as separate strings?
 
Posts: 115 | Thanked: 342 times | Joined on Dec 2010
#6
Originally Posted by marmistrz View Post
Yes. this is what I'm talking about

I wanna read this with getline and have from

Code:
blacklist = Conky mediaplayer
Code:
blacklist
Code:
Conky
Code:
mediaplayer
as std::string each

Do you understand?
Code:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

vector<string> split(string &str)
{
	vector<string> tmp;
	stringstream stream;

	stream << str.erase(9,2);
	string cache;
	while(getline(stream, cache, ' '))
	{
		tmp.push_back(cache); 
	}
	return tmp;
}


int main(void)
{
std::string d= "blacklist = Conky mediaplayer";
vector<string> blisted = split(d);
cout << blisted[0]; //blacklist
cout << blisted[1]; //Conky
cout << blisted[2]; //mediaplayer
}

Last edited by NIN101; 2011-10-11 at 16:18. Reason: unneeded header
 
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#7
Originally Posted by NIN101 View Post
Code:
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

vector<string> split(string &str)
{
	vector<string> tmp;
	stringstream stream;

	stream << str.erase(9,2);
	string cache;
	while(getline(stream, cache, ' '))
	{
		tmp.push_back(cache); 
	}
	return tmp;
}


int main(void)
{
std::string d= "blacklist = Conky mediaplayer";
vector<string> blisted = split(d);
cout << blisted[0]; //blacklist
cout << blisted[1]; //Conky
cout << blisted[2]; //mediaplayer
}
What happens if the word before '=' is greater than 9 characters in length? It might be better to use the string::find_first_of method to find the position of the first whitespace character.
__________________
'Men of high position are allowed, by a special act of grace, to accomodate their reasoning to the answer they need. Logic is only required in those of lesser rank.' - J K Galbraith

My website

GitHub

Last edited by marxian; 2011-10-11 at 16:53.
 
erendorn's Avatar
Posts: 738 | Thanked: 983 times | Joined on Apr 2010 @ London
#8
i think you could even go with something like:
Code:
while (stream)
{
    cache << stream;
    tmp.push_back(cache);
}
because streams stop at each space while outputing to strings (and discard the space).
 
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#9
 

The Following 2 Users Say Thank You to nicolai For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 02:54.