Jump to content
  • Advertisement
Sign in to follow this  
Sijmen

Going back from C# to C++

This topic is 5053 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you want so, you can skip this boring background and head to the question. Quite some time ago I stopped using C++ for a while and started doing some/much programming in C#, using SDL.NET and later my own library ManagedGL. As I got interested in 3D more and more, and the C++ code in the 'MUD Game Programming' book looked really appealing again, I recently partially switched back to C++. But now there's a problem: I never really used the C++ standard library besides some basic cin and cout. Even worse, I was actually coding C+classes, mainly using cstdlib. Using the NeHe tutorials I game quite far, and it looks promising. Now I'm able to draw textured quads and such, I'm going to make a simple maze game - which generally really are 2D besides the graphics, which is nice, because I do know how to make games in general. - question The actual problem: string parsing. Let's say I have this file:
WWWWWWWWWWW
W*........W
WWWWWWW...W
W...@.....W
WWWWWWWWWWW
This is my map. A dot is nothing, a W is a wall, the @ is the start position, and the * is the end position, very simple, so to say. In C#, I'd do it like this:
for(int y=0; y<mapHeight; y++)
	string line = theFileReader.ReadLine();
	for(int x=0; x<mapWidth; x++)
		map[x,y] = createTheTileFromChar(line[x]);
I know how to change this array to a single-dimension array using the y*w+x thingy, but it's really about the string parsing - if you could call it so. How to do this in C++?, using std::string? Then, I'd like to know in what ways these functions are available in C++: TextStream.ReadLine() with std::istream String.Trim() with std::string String.Split() with std::string Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You'd be better off writing these functions yourself, very fast:

vector<string> Split(std::string str, char del) {
vector<string> strVector;
string temp;

for(int i=0; i<str.length(); i++) {
if(str == del) {
strVector.push_back(temp);
temp = "";
continue; // next loop-round immediately
}
temp += str;
}
return strVector;
}


Something like this, not sure if that vector<> part will work, though.

Share this post


Link to post
Share on other sites
Don't use istream getline like Pipo suggested, it's for char*. Use "std::getline(inputStream, string);"

And for split and trim, see latest version of Boost and it's String Algorithms Library:
http://www.boost.org/doc/html/string_algo.html

Share this post


Link to post
Share on other sites
Thanks for the info. That'll get me going.

/edit:

How would you implement that map loading thing? Using a filestram or something lik that? Then how to read a single character from it?

Share this post


Link to post
Share on other sites
You'll soon discover that string manipulation in C++ isn't anywhere near as nice as it is in C# [smile]

Reading a line is easy:

std::istream in_stream;
std::string out_string;
std::getline(in_steam,out_string);


String.Trim() can be done like this:

std::string str;
str = str.substr(0,str.find_last_not_of(' '));
str = str.substr(str.find_first_not_of(' '));


and String.Split() is a little trickier. You can either get a char* pointer to it's contents with str.c_str() and use strtok() as mentioned (be careful though, the pointer returned by str.c_str() is only guarentteed to be valid if str isn't modified). Or alternatively you could do it like so:

std::list<std::string> split(const std::string& str, const std::string& separators)
{
std::list<std::string> tokens;
for(std::string::size_type index=0 ; index!=std::string::npos ;)
{
std::string::size_type first = str.find_first_not_of(separators,index);
std::string::size_type index = str.find_first_of(separators,first);
if(first!=std::string::npos) tokens.push_back(str.substr(first,index-first));
}
return tokens;
}



EDIT: Wow, I'm slow, there was only 1 reply when I started typing this! [crying]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!