Archived

This topic is now archived and is closed to further replies.

abdulla

odd STL problems

Recommended Posts

this is the class: class ngene::configure { public: explicit configure(const char*, const char*); public: int width, height; protected: std::ifstream infile; std::ofstream outfile; std::string feed; public: char* valueof(const char*); }; here''s the expanded functions: ngene::configure::configure(const char* commandline, const char* filename) : infile(filename, std::ios_base::binary), outfile("dump.txt") { // if(!file) copy file from store char buffer; while(infile.get(buffer)) feed += buffer; std::transform(feed.begin(), feed.end(), feed.begin(), tolower); outfile << valueof("width"); // outfile << valueof("height"); MessageBox(NULL, feed.c_str(), "commands", MB_OK); } char* ngene::configure::valueof(const char* token) { std::string editbuffer = feed; char* substring = strstr(feed.c_str(), token); char* keystring = strtok(substring, ";"); char* value = strtok(keystring, " "); for(UCHAR i = 1; i < 3; ++i) value = strtok(0, " "); feed = editbuffer; return value; } there''s the problem, if i remove the lines in valueof that have "std::string editbuffer = feed" and "feed = editbuffer" it works but modifies the feed (which i believe it shouldn''t), and if i reinstate feed with the editbuffer backup it spits out garble, when i use strings substr and find functions it does the same thing, it spits out garble and accorinding to the debugger comes out with an impossible position (something like 43 million), is there something wrong with my implementation or is it something with the way i''m coding? here''s the data from the file its reading: // START /* display settings */ width = 1152; height = 768; // END! thank you for your time, abdulla

Share this post


Link to post
Share on other sites
I''ll make a couple of observations :

1) using the c_str() method of the std::string class gives you a pointer to the raw string inside the class ( for compatibility with things that need char* ) and NOT a copy

If strstr or strtok modify the string then it will affect the original.

Another way to do this is to use stringstreams and extract the data

eg create a string stream from the passed in string
std::stringstream buffer( token ); // this may take a little coercing

and then extract the data, if separated by whitespace it is just a matter of using the extraction operator

otherwise there are methods for extracting using a different delimiter - eg your semicolon ussage.

Share this post


Link to post
Share on other sites
still the call to strstr should make a new copy of the string returned as a substring, after using that function it also eats up the data in odd ways

Share this post


Link to post
Share on other sites
I''m not so sure that strstr makes a copy of the string.

If it does then there is a memory leak in that code.

Its far more likely that it returns a pointer to the address in the string which has the substring.

You can easily check this when debugging - look at the base address of the string and its length, if the returned pointer is within base address to base address + len then it is pointing to the existing string.

Share this post


Link to post
Share on other sites