Crashed. Even if it tries to read just one chromosome and fitness from that DEAD file it crashes.
(Sigh)
Crashed. Even if it tries to read just one chromosome and fitness from that DEAD file it crashes.
(Sigh)
Error checking is unnecessary if you use the read statement as the while conditional, which is what the poster was doing.Originally Posted by Bench82
Extracting from an STL container is not the same as extracting from an istream. The Vector will work because the iterator position can be checked before looping, using vector::end().
An istream cannot be checked in the same way because there is no iterator to denote "one past the end". I've reposted this to include the check - this one will not overrun.
Code:while (file_to_read) std::string s; std::string::size_type start(0), index; std::getline(file_to_read, s); index = s.find('_'); if (index == std::string::npos) break; std::string chromosome_read(s, start, index); std::string fitness_read(s, index);
That is true, but he was also using the while conditional to search for a character in the stream at the same time, and then assign the result to another string. This won't work because the final time the conditional is called, that character will not exist.Originally Posted by 7stud
it would be OK if he was simply using
Even then, before doing any kind of assignment (based on a search for a char) to other strings in the loop, there should still be some kind of check in order to protect against overrun.Code:while(getline(s, file_to_read) )
Last edited by Bench82; 02-20-2006 at 12:44 PM.
I've tried your loop, Bench. It still crashes, unfortunately.
Then I'm baffled It compiles and runs on my system.Originally Posted by Onions
(Which is MSVC++ 2003 - although as far as I'm aware, I've not used any non-standard code)
Last edited by Bench82; 02-20-2006 at 01:01 PM.
Well, it compiles and runs, yes. But after it has made a few enemies the game crashes for random reasons again.Originally Posted by Bench82
It's possible that what you write to the file is not what you think, and therefore when you read the data into your vector, it's corrupted data. Personally, I don't like the way you are formating your data when you write to the file. Why don't you do this instead:
22222222222222 3443
Then you can use >> to read in the data, and there won't be any whitespace or newlines in the data you read. getline() grabs everything until the delimiter, which may be grabbing some whitespace after 3443, and maybe that whitespace is causing the crashes.
Last edited by 7stud; 02-20-2006 at 01:18 PM.
So, if I then opened the DEAD file to look at what had happened would it look like
Or would it still beCode:111111111111 223 222222222222 342 111111111111 112
It's not hugely important, I guess. I'm just trying to understand how you think I should do it.Code:111111111111 223 222222222222 342 111111111111 112
Also - how will there not be any whitespace? There is still a gap between the chromosome and the fitness, isn't there?
I've changed it to this:
And the DEAD file starts off as just:Code:while (file_to_read) { file_to_read >> chromosome_int_read; file_to_read >> fitness_string_read; chromosome_int_array.push_back(chromosome_int_read); fitness_string_array.push_back(fitness_string_read); }
But it is now reading in that chromosome and that fitness twice. What should I use as the terminating condition for this new code?Code:111111111111 50
(Also, thanks for the continuing help)
Wrong choice. If you do that, then you have to add some end of file error checking inside the while loop. Keep your read statement as the while conditional.I've changed it to this:
while (file_to_read)
That's what I was envisioning. However, when you use >> to read in data, it doesn't matter whether the data is on separate lines or not--the only thing you need is alternating strings. The >> operator skips all whitespace(spaces, tabs, and newlines).Or would it still be
111111111111 223
222222222222 342
111111111111 112
If you look at the file that your program wrote, you cannot tell what is at the end of the line. Carriage returns between lines are invisible. So if you see this in the file:
111111111111 223
222222222222 342
can you tell if there are 10 spaces after the first line followed by a carriage return, or if there is only a carriage return at the end of the line? If there are 10 spaces, then getline will grab:
and stick it in your string variable. Will having those spaces in the string cause your program to crash?Code:"223 "
Try this: after your program writes the strings to the file, and after you finish reading the strings from the file and putting them in your vector, output the contents of the fitness_string_array vector. Additionally, after every string add "<----". For instance, do something like this:
cout<<vector[i]<< "<----"<<endl;
If there are no spaces at the end of the data, you will see:
If there are spaces at the end of the data, you will see:Code:223<----
If everything looks ok after that test, then you have to consider the near certainty that the problem is not in that loop at all. Somewhere in your code, you may be trying to access elements that are out of bounds in your vector, which could cause your program to crash. Or, you may be trying to access index positions in the strings themselves that are of bounds.Code:223 <----
Last edited by 7stud; 02-20-2006 at 10:01 PM.
I finally fixed this a couple of days ago.
I gave up on storing the information in a text file and used some vectors instead. Then I made a function which copies the vectors to an (otherwise unused) text file in a decent format so that I can view and analyse what happened with the genetic algorithm.
Thanks to everyone who helped, particularly 7stud. I really appreciate you taking the time to lend a hand.
Cheers.