Jump to content
  • Advertisement
Sign in to follow this  

Writing a string array

This topic is 4674 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

Oh yes :P I'm having troubles again. :( After all your guys' help, I finaly got everything working, except writing a string array to disk. So I used the method I was using, which everyone helped me with, yet it doesn't work :( So, here goes :P
string *IDs = new string[13];

	IDs[0] = "0";
	IDs[1] = "1";
	IDs[2] = "2";
	IDs[3] = "3";
	IDs[4] = "4";
	IDs[5] = "5";
	IDs[6] = "6";
	IDs[7] = "7";
	IDs[8] = "8";
	IDs[9] = "9";
	IDs[10] = "10";
	IDs[11] = "11";
	IDs[12] = "12";

	for(int i = 0;i<fileStruct.tileCount;i++)
		UINT size = UINT(fileStruct.mapIDs.size());
		// Write the length of the string to the file
		writer.write((char*)&size, sizeof(UINT));
		// Write the string to the file


This method worked fine for a single string, and i figured it should work for an array. But no :P It works until I hit the first line of the for loop, where it does UNIT(...). It breaks with this error:
Unhandled exception at 0x1048b2ba (msvcp80d.dll) in Nocturne.exe: 0xC0000005: Access violation reading location 0x00000018.
So..what do I do? fileStruct.tileCount is initialized, and for this is 13. And yes, i checked this, and it is 13 going into the loop. Anyhelp would be awesome. Thanks!

Share this post

Link to post
Share on other sites
fileStruct.mapIDs appears to be uninitialized (a null pointer). Make sure that you're overwriting it with the IDs pointer somewhere.

Otherwise the code looks fine too me..

Share this post

Link to post
Share on other sites
You say you're trying to write an array of strings to disk, but I don't know what fileStruct is, or that mapIDs structure, either. You didn't actually touch your "array of strings"

std::vector<string> ids(10);

ids[0] = "zero";
ids[1] = "one";
ids[2] = "two";
ids[3] = "three";
ids[4] = "four";
ids[5] = "five";
ids[6] = "six";
ids[7] = "seven";
ids[8] = "eight";
ids[9] = "nine";

for( std::vector<string>::iterator iter = ids.begin(); iter != ids.end(); ++iter )
//print each string in 'ids', one to a line
file << *iter << '\n';

Share this post

Link to post
Share on other sites
You can also use std::copy to "copy" the container to an output stream:

std::copy( ids.begin(), ids.end(), std::ostream_iterator<string>( file_stream, "\n" ) );

Where "\n" is the delimiter.

That gets rid of the whole for loop.

Share this post

Link to post
Share on other sites
is there ever a case when the ids wont be integers?

if not then you can just make it a int array and simplfy both your loading and saving code...( that is, if you are eventually going to convert them to an int )

unless the fact that they are strings is important

Share this post

Link to post
Share on other sites
Oie. Lol. I have to stop posting code when I wake up :P Of course, i forgot to assign it to my struct. Sorry for wasting your time guys...And ya, it will hold string things instead of "1", "2", etc. It will hold "ID_GRASS" or w/e, so a int array is out of the question.

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!