🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

File I/O routines are a pain in the ***

Started by
4 comments, last by The Madman 24 years, 10 months ago
OK, stupid math error, it was SUPPOSED to be 315 bytes but I made a stupid error. But that still doesn't explain WHY its gives out garbage.

------------------
That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D

That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D
Advertisement
Here's your problem buddy....you increment the pointer...and *poof* you expect it to remember where it begins??? Well, unless you left out part of you code, you obviously do. You have to make sure you keep track of where the array begins and ends if you are going to increment it in this fashion. If you were to use array, you would have this problem, but since you're doing array++, unless you back it up, it's gone forever… Now, I could be wrong here, and missed something, but there really isn't enough unified code to tell. (At least not for me.) Sorry if this doesn't help.
Sorry, I left out some words. If you use array, you do NOT have to keep track of the beginning. Otherwise, make a char *temp, and just have temp = array…then when you're done stuffing it, array = temp, and you're back to the beginning.<BR>
OK, here's the deal. I got it working pretty good, but I modified it a bit. First I used the old C file routines, like fopen(). Second, I cleaned up the code and I modified it so that it doesn't use pointers but deals with the arrays themselves. Finally, I got it to work except for a slight matter. Here's the main() function:

code:
// Main functionvoid main(void){	char choice=0;	char x,y;	char array[316];	char newline=0;	CLevelDesc level;	level.LoadLevelFile();	// Now get the user's input	while(choice!='3')	{		// Now get what the heck the user wants to do		printf("\nEnter 1 for reading, 2 for writing, and 3 to quit.\n");		choice=getche();		printf("\n");		switch(choice)		{		case '2':			for(y=0; y			{				newline++;				array[((y*CELL_X)+x)+newline-1]='\n';				printf("\n");				for(x=0; x				{					array[((y*CELL_X)+x)+newline-1]=getche();				}			} // end for			array[315]='\n';			level.WriteNewLevel(array);			break;		} // end switch	}// end while	level.UnloadFile();} // end main()

It works pretty well, except on the first line instead of getting a newline two of the lines. Here is the first line


ddddddddddddddddddddÖdddddddddddddddddddd


Could someone help me?

------------------
That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA!

[This message has been edited by The Madman (edited August 08, 1999).]

That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D
Hi people, I'm working on my next game already. I'm trying to use Andre's strategy in Underworld and not use hard coded data, but use an external file that contains the level data. Unfortunately, this is giving me pain. I am using my preferred file loading functions, the mmio type. I am also using introductory C++/OOP, as I'm using classes. Here's my code for loading the array with data and stuffing it into the class function:
switch(choice)
{
case '2':
for(y=0; y{
for(x=0; x{
*array=getche();
array++;
}
*array='\r';
array++;
printf("\n");
} // end for
level.WriteNewLevel(array);
break;
} // end switch

The variable "array" is a character pointer with all the space needed allocated. Here's the class function, WriteNewLevel():

void CLevelDesc::WriteNewLevel(char *data)
{
// go to the end of the file
mmioSeek(file, 0, SEEK_END);

// Write the data to file
mmioWrite(file,data,(CELL_X+1)*(CELL_Y));
}

The problem is that my file now holds 315 letters of garbage where it should hold 320 letters of whatever I specify (right now I'm trying to use just one character that is solid throughout the file.) Could someone help me?

------------------
That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D

That's how you do it, but that's just the opinion of a MADMAN!!! BWAHAHAAHAHA! :D :D :D
Are you getting the ...dddÖddd... effect on every line, or just the first?

Can't really see what your code is doing, because it's trying to parse it for html... look at your for loops

When you're showing code in your posts, a good suggestion is:

<code>
code goes here...
</code>

White Fire

This topic is closed to new replies.

Advertisement