Skip to main content
gamedev.net
🔒 Locked

C++ and string conversion

Started by BOnions Nov 1, 2005 at 1:21 PM 1 replies 550+ views
Original Post
BOnions
BOnions
Hi there, I'm trying to implement genetic algorithms into a game I'm making. I have a problem with the way I'm storing and accessing information in text files. This is what I'm doing, basically: - Every enemy has an array of integers as a variable (their chromosome). - These arrays are stored by writing them as one line each to a text file. So it looks like:

1111111111   (a simple robot)
1123112213   (a slightly more advanced one)
- These are then accessed by being read into an array of strings (each line is one chromosome, which is one element of the array). And so...my problem is that I now need to rip apart these strings and get them back into arrays of integers. So that after I have stored them and read them back, I can then mutate the values and create new "evolved" enemies. I've just spent a dreadful couple of hours trying to figure out how I mght be able to accomplish this. The strings in C++ seem a lot less flexible than in Java, that's for sure. Can anyone offer any guidance? Much appreciated.
silverphyre673
silverphyre673
I would suggest that you instead store the chromosome as a vector of integers. Convert each ASCII value from the text file to an integer by subtracting '0' from it. So if the first value of the chromosome was 4, then you store the value '4' - '0'.
my siteGenius is 1% inspiration and 99% perspiration
misterPhyrePhox
misterPhyrePhox
Streams can automagically read integers and the like. You use the stream input operator, >>. Depending on the data type you use with >>, >> basically starts at the current stream position, and searches for a delimiter -- a character that would represent the end of one piece of data, such as a space or a newline. Then the >> converts the piece of data it found into the datatype you are using with it. The problem with the way you are storing your data is that it contains no delimiters, so the stream couldn't distinguish one piece of data from the next. So I suggest you store your data like: 1 20 49 182, whatever, and then use a stream to parse it.
Here is a little bit of source:
#include <iostream>#include <fstream>#include <sstream>#include <string>#include <vector>void main() {	std::ifstream file("Data.txt"); // Create a file input stream	if(file.fail()) { // Check for failure!		std::cout << "Failed to load \"Data.txt\"\n";		return;	}	// We will read a line of data into this string	std::string line = "";	while(!file.eof()) {		std::getline(file, line); // Read the line		// I don't know if you know what the two things below are, so I'll		// explain them a bit...		// A string stream is just like any other stream, except that it		// performs actions on a string... so lets feed it the line we just		// got, and then use it to parse the data		std::istringstream parser(line);		// A vector is a container class from the STL that allows you to		// add and remove elements from it at runtime, rather than an array		// where you have a fixed size... look it up on the web for more info		std::vector<int> data;		while(!parser.eof()) {			int buffer; // Create a temporary buffer			parser >> buffer; // Read the data into the buffer			data.push_back(buffer); // Add the data to the vector		}				// Now we have a vector full of the data, yay!		// We can do whatever we want with it...	}	file.close(); // Clean up!}

Hope that helps!

Topic Locked

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

Sign in to reply to this topic.