Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 The Madman   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 August 1999 - 06:48 PM

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


Sponsor:

#2 The Madman   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 August 1999 - 06:09 AM

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


#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 05 August 1999 - 07:13 AM

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[i], 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.

#4 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 05 August 1999 - 07:16 AM

Sorry, I left out some words. If you use array[i], 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.


#5 The Madman   Members   -  Reputation: 122

Like
Likes
Like

Posted 05 August 1999 - 02:03 PM

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 function
void 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).]


#6 White Fire   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 August 1999 - 06:48 PM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS