🔒 Locked
std::string manipulation help
Advertisement
Original Post
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.
string::find
Searches a string in a forward direction for the first occurrence of a substring that matches a specified sequence of characters.
buffer.find( Test) returns the position of first match or std::string::npos
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
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
from the top of my head
while( FILE.getline( buffer, 1000)) if( buffer.find( Test) != std::string::npos) { //do this } else { //do that }
well that would imply that buffer isn't a std::string then...
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
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
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.
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
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
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]
Quote:You can pass it a C-style string too. The compiler will create a std::string out of the C-style string you pass.
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
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");
}
}
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");
}
}
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.:
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.:
You can also use variation of the above technique with the standard library algorithms.
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>()); //formattedYou 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.
Advertisement