Sign in to follow this  
Squishy

std::string manipulation help

Recommended Posts

Squishy    122
I have 2 std::strings called buffer and Test. I want to see if Test appears in buffer i was just wondering how this could be done in c++

Share this post


Link to post
Share on other sites
JohnBolton    1372
Straight out of the docs. Took about 15 seconds...

string::find

Searches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters.

Share this post


Link to post
Share on other sites
Squishy    122
Right this seems to be going in the right direction but im still not their yet

while (! FILE.eof() )
{
FILE.getline (buffer,1000);
if (std::string::find(buffer,Test))
DO THIS
else
DO THAT



}

This is basically hwat im trying to get it to perform im very new to c++ and would apreciate any feed back the previouse other replys i wasnt sure how to implement

Share this post


Link to post
Share on other sites
Squishy    122
Ok im officially hopeless i have no idea hwta im doing i should never have taken this on but now its annoying me

This is the whole function

void IRCBot::Remscans(std::string Test, std::string nick)
{
char buffer[1000];
std::ifstream File("SCANS");

while (! File.eof() )
{
File.getline (buffer,1000);
if( buffer.find( Test) != std::string::npos)
SEND_NOTICE(nick, "WORKING");
else
SEND_NOTICE(nick, "NOT WORKING");



}
}

im probebly doing somthing fundementaly stupid i usually am

Share this post


Link to post
Share on other sites
Sneftel    1788
Why are you declaring buffer as an array of 1000 characters, rather than as a std::string?

Share this post


Link to post
Share on other sites
DarkHamster    180

char buffer[1000];

Obviously buffer isn't an std::string, you define it as a char array. Try making a string from that (e.g. "std::string buf_str = std::string(buffer)") and then using std::string::find.

Share this post


Link to post
Share on other sites
Squishy    122
that was my first port of call however when passing buffer into function SEND_NOTICE buffer must be a std::string as thats what that function requires

Share this post


Link to post
Share on other sites
Squishy    122
ok again im sorry to be a complete ignorent sod

but i really dont know c++ all that well im adapting a peice of code already writen and trying to work my way through learning as i go along.

would it be possible for someone to tell me how i should change my code so that it will work cos i really have no idea

Share this post


Link to post
Share on other sites
Dave Hunt    4872
Quote:
Original post by Dave Hunt
That's what the std::string::c_str() method is for.


It appears I misread your post. I thought you were saying buffer had to be a char array, since that's how it was declared. Please ignore this piece of useless information.

And don't apologize for a lack of knowledge. You are trying to learn and that's nothing to apologize about.

Unfortunately, I have to leave, so I can't add to your knowledge right now. If you haven't worked out a solution by the time I'm back on line, I'll give you a hand.

edit - since when does apologize have 2 'p's?

[Edited by - Dave Hunt on March 29, 2005 9:44:27 AM]

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by Squishy
that was my first port of call however when passing buffer into function SEND_NOTICE buffer must be a std::string as thats what that function requires
You can pass it a C-style string too. The compiler will create a std::string out of the C-style string you pass.

Share this post


Link to post
Share on other sites
Squishy    122
I have been playing about a bit and i was wondering

Is it possible to search my buffer an array of chars with a std::string so that my code would be the same apart from.


void IRCBot::Remscans(std::string Test, std::string nick)
{
char buffer[1000];
std::ifstream File("SCANS");

while (! File.eof() )
{
File.getline (buffer,1000);
if( buffer.find( Test) != std::string::npos) <<<---- This Line Here
SEND_NOTICE(nick, "WORKING");
else
SEND_NOTICE(nick, "NOT WORKING");



}
}

Share this post


Link to post
Share on other sites
achacha    146

void IRCBot::Remscans(std::string Test, std::string nick)
{
std::string buffer;
std::ifstream File("SCANS");

while (! File.eof() )
{
File.getline (buffer,1000);
if(std::string::npos != buffer.find(Test))
SEND_NOTICE(nick, "WORKING");
else
SEND_NOTICE(nick, "NOT WORKING");
}
}


Share this post


Link to post
Share on other sites
snk_kid    1312
Okay what are we all smoking today, there is a free-function in the string header that reads in lines from a stream directly into a std::basic_string i.e.:


std::string s;
...
std::getline(some_stream, s);


Also the last parameter takes a delimiter which has a default value.

Also another alternative is to use stream iterators, either istreambuf_iterator (unformatted input) or istream_iterator (formatted input) i.e.:


#include <iterator>

std::string s((std::istreambuf_iterator<char>(some_stream)),
std::istreambuf_iterator<char>()); //unformatted

std::string s2((std::istream_iterator<char>(some_stream)),
std::istream_iterator<char>()); //formatted


You can also use variation of the above technique with the standard library algorithms.

Share this post


Link to post
Share on other sites
Squishy    122
ok much apreciater all the feed back but i was able to just set another array of chars then set the passed in std::string to this and i can manipulate the char array no problem. And it only took me 3 hours \o/

Thanx again for all the feed back

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this