Skip to main content
GameDev.net gamedev.net
🔒 Locked

std::string manipulation help

Started by Squishy Mar 28, 2005 at 2:53 PM 19 replies 3.4k views
Original Post
Squishy
Squishy
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++
JohnBolton
JohnBolton
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.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
DigitalDelusion
DigitalDelusion
buffer.find( Test) returns the position of first match or std::string::npos
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Squishy
Squishy
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
DigitalDelusion
DigitalDelusion
from the top of my head
while( FILE.getline( buffer, 1000))  if( buffer.find( Test) != std::string::npos)  {  //do this  }  else  {  //do that  }
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Squishy
Squishy
i tryed that i currently am getting the error

error C2228: left of '.find' must have class/struct/union type
DigitalDelusion
DigitalDelusion
well that would imply that buffer isn't a std::string then...
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Squishy
Squishy
ok thanx for your time i will go back and check what buffer is
Squishy
Squishy
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
Sneftel
Sneftel
Why are you declaring buffer as an array of 1000 characters, rather than as a std::string?
Dave Hunt
Dave Hunt
As DigitalDelusion stated, buffer is not of type std::string.
DarkHamster
DarkHamster

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.


"There is no dark side of the moon really,
As a matter of fact, its all dark."
Squishy
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
Dave Hunt
Dave Hunt
That's what the std::string::c_str() method is for.
Squishy
Squishy
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
Dave Hunt
Dave Hunt
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]
Evil Steve
Evil Steve
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.
Squishy
Squishy
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");



}
}
achacha
achacha
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");  }}

++Alex
snk_kid
snk_kid
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>()); //unformattedstd::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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.