<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Saving and Loading Problem</title>
<description>The following functions in WiiBreaker dont appear to be working right for me:

[code]

FILE *pattern;


void SavePattern(){

pattern = fopen(&quot;fat:/apps/Wiibreaker/data/pattern1.pattern&quot;,&quot;rb&quot;);

for(int i = 0; i &lt; 30; i++){
putc(brickx[i], pattern);
putc(bricky[i], pattern);
}

fclose(pattern);

}

void LoadPattern(){

pattern = fopen(&quot;fat:/apps/Wiibreaker/data/pattern1.pattern&quot;,&quot;rb&quot;);

for(int i = 0; i &lt; 30; i++){
brickx[i] = getc(pattern);
bricky[i] = getc(pattern);
}

fclose(pattern);

}
[/code]

Can anybody tell me what I&#039;m doing wrong (and why)?

Thanks in advance for any and all help recieved. I truly appreciate it.

Arikado</description><link>http://forum.wiibrew.org/read.php?11,8916,8916#msg-8916</link><lastBuildDate>Sat, 11 Apr 2026 07:22:12 +0200</lastBuildDate>
<generator>Phorum 5.2.23</generator>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9465#msg-9465</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9465#msg-9465</link><description><![CDATA[ It&#039;s even more trivial to just use snprintf. I would recommend this over itoa.]]></description>
<dc:creator>Tantric</dc:creator>
<category>Coding</category><pubDate>Wed, 04 Feb 2009 08:19:52 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9464#msg-9464</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9464#msg-9464</link><description><![CDATA[ I&#039;d like to point out that writing itoa is fairly trivial, or you can use the K&R version linked from wikipedia.]]></description>
<dc:creator>iofthestorm</dc:creator>
<category>Coding</category><pubDate>Wed, 04 Feb 2009 08:14:10 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9172#msg-9172</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9172#msg-9172</link><description><![CDATA[ If you need to know exactly how long the string buffer needs to be, just bring out good old logarithms, log10(number)=number of digits. Just remember to account for the string terminator character too.]]></description>
<dc:creator>henke37</dc:creator>
<category>Coding</category><pubDate>Sat, 31 Jan 2009 11:49:27 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9148#msg-9148</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9148#msg-9148</link><description><![CDATA[ If you want to maintain a human readable file format, so you can edit the file with any text editor, you can write integers to a file as a char array (string), and when you read them back, convert the string back to an integer using <a href="http://en.wikipedia.org/wiki/Atoi" rel="nofollow">atoi</a>.<br /><br />To convert the integer to a string for saving, you would usually use <a href="http://en.wikipedia.org/wiki/Itoa" rel="nofollow">itoa</a>, but devkitPro/libogc do not include this function, as it is non standard, and the devs think it is useless! However, you can use sprintf instead.<br /><br /><pre class="bbcode">
/* NOT a working example, just a hint to get you started */

/* This will save each integer on a new line */
/* FILE *f is a stream you have opened elsewhere */
void write_int_to_file(int value, FILE *f) {
    char buffer[12]; /* You will need some memory to hold the string */
    sprintf(buffer, "%d\n", value); /* print the numerical representation of the int into the buffer */
    fputs(buffer, f); /* Write the buffer to the file stream */
}

/* This will return the integer value in the array */
int read_int_from_array(char *array) {
    return atoi(array);
}

void load_file() {
    /* Open the file stream here - I&#039;ll call it inFile */
    
    char buffer[12]; /* buffer to read each line of the file to */
    while (fgets(buffer, 12, inFile) != NULL) {
        int value = read_int_from_array(buffer);
        /* Do something with value */
    }
}
</pre>]]></description>
<dc:creator>DrTwox</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 23:36:05 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9133#msg-9133</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9133#msg-9133</link><description><![CDATA[ <blockquote class="bbcode"><div><small>Quote<br /></small><strong>scanff</strong><br /><i>[...]</i></div></blockquote>If you want to know, I&#039;m going to develop the pc-pattern-viewer in dotNet/Mono, which has some simple endian features. C# is more simple compared to C(++).]]></description>
<dc:creator>Dykam</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 19:02:49 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9131#msg-9131</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9131#msg-9131</link><description><![CDATA[ If your are creating an editor on windows you will need to deal with endianess like henke37 mentioned. Here&#039;s a function that will help you out.<br /><br /><pre class="bbcode">

unsigned long swap2 (unsigned long number)
{
   return (((number&0x000000FF)&lt;&lt;24)+((number&0x0000FF00)&lt;&lt;8) + ((number&0x00FF0000)&gt;&gt;8)+((number&0xFF000000)&gt;&gt;24));
}</pre><br /><br />If you create the map on Win and load on Wii you will need to do this -<br /><pre class="bbcode">
for(int i = 0; i &lt; 30; i++){
    brickx<i> = swap2 (brickx<i>);
    bricky<i> = swap2 (bricky<i>);
}
</i></i></i></i></pre><br />Also don&#039;t forget that an int is 4 bytes on Windows. I think it&#039;s 4 bytes on the Wii also.]]></description>
<dc:creator>scanff</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 18:25:34 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9130#msg-9130</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9130#msg-9130</link><description><![CDATA[ Alright ekeeke I did what you told me and it worked. Not only does it work, its a lot faster than it was before.<br /><br />Sorry for my ignorance as I took brickx[0] a little to literally and assumed I would end up writing bricx[1], brickx[2], brickx[3]... You get the picture. So I attempted to use a for loop instead. I gave myself a little crash course in C pointers like you reccomended, and why everything works or doesnt work makes alot more sense to me.<br /><br />Up until a couple days ago I didnt know FILE classes or fopen() even existed. I&#039;d like to personally thank everyone who posted in this topic for getting me as far as you all did. Consequently, WiiBreaker now has a fully working level editor and I have a tool to read pattern files. Once again, thank you to everyone, I would not have gotten even a quarter of the way without all of your help.]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 16:54:50 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9129#msg-9129</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9129#msg-9129</link><description><![CDATA[ lol, sorry but you put some random code again, can&#039;t you just copypaste what I gave you<br />don&#039;t want to sound rude but you should REALLY try first to understand what you are coding<br /><br /><br />first, there was no more "for" loop needed<br /><br />and I didn&#039;t write<br /><br />&brickx<i><br /><br />but<br /><br />&brickx<b>[0]</b><br /><br /><br />see the difference ?<br />(same for bricky)<br /><br />&brickx<i> is a pointer INSIDE the brickx array, i being the offset<br />&brickx[0] is a pointer to the BEGINNING of the array<br /><br />by doing what you did, you were writing 30x times the content of the file in brickx, at different offsets<br />and the result was that:<br />1/ you ended up overwriting all array values with brickx[0] value<br />2/ you are writing outside the allocate memory<br /><br />which is now worst than before<br /><br />hope this is more clear now<br /><br />I&#039;d avise you to learn C pointer as soon as possible, it is a little bit complicate first but would be very handy later</i></i>]]></description>
<dc:creator>ekeeke</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 16:03:19 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9128#msg-9128</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9128#msg-9128</link><description><![CDATA[ koopa, I tried both of your functions <i>exactly</i> as you posted them and they return or load values far different then the saved values.]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:50:31 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9127#msg-9127</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9127#msg-9127</link><description><![CDATA[ you mixed it up again.<br /><br />it should either be:<br /><pre class="bbcode">
void SavePattern(){
	
	FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
    if (pattern){
	for(int i = 0; i &lt; 30; i++){
    fwrite(&brickx<i>, sizeof(int), 1, pattern);
    fwrite(&bricky<i>, sizeof(int), 1, pattern);
	}
    fclose(pattern);
    }
	
	}
	
	void LoadPattern(){
	
	FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
    if (pattern) {
	
	for(int i = 0; i &lt; 30; i++){
    fread(&brickx<i>, sizeof(int), 1, pattern);
    fread(&bricky<i>, sizeof(int), 1, pattern);
    }
    fclose(pattern);
    }
	
	}

</i></i></i></i></pre><br />or<br /><br /><pre class="bbcode">
void SavePattern(){
	
	FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
    if (pattern){
	
    fwrite(&brickx[0], sizeof(brickx), 1, pattern);
    fwrite(&bricky[0], sizeof(bricky), 1, pattern);
	
    fclose(pattern);
    }
	
	}
	
	void LoadPattern(){
	
	FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
    if (pattern) {
	
	
    fread(&brickx[0], sizeof(brickx), 1, pattern);
    fread(&bricky[0], sizeof(bricky), 1, pattern);

    fclose(pattern);
    }
	
	}

</pre>]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:41:26 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9126#msg-9126</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9126#msg-9126</link><description><![CDATA[ I dont mean to continually post random bits of code, but changing to your method ekeeke doesnt appear to be working.<br /><br /><pre class="bbcode">
void SavePattern(){
	
   FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
    if (pattern){
    for(int i = 0; i &lt; 30; i++){
    fwrite(&brickx<i>, sizeof(brickx), 1, pattern);
    fwrite(&bricky<i>, sizeof(bricky), 1, pattern);
    }
    fclose(pattern);
    }
	
  }
	
   void LoadPattern(){
	
   FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
    if (pattern) {
    for(int i = 0; i &lt; 30; i++){
    fread(&brickx<i>, sizeof(brickx), 1, pattern);
    fread(&bricky<i>, sizeof(bricky), 1, pattern);
    }
    fclose(pattern);
    }
	
  }
</i></i></i></i></pre>
<b>EDIT:</b> However it did fix my pattern tool.]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:32:03 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9125#msg-9125</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9125#msg-9125</link><description><![CDATA[ It worked because you probably simply tested the save/load functions consecutively, without doing anything else in between (that&#039;s way you saved data that was outside arrays memory but when you load the content o fthe file, you just ended up overwriting memory with the same values)<br /><br />or you just get lucky<br /><br />but believe me, writing 3600 bytes in a 120 bytes array only lead to one thing: memory corruption]]></description>
<dc:creator>ekeeke</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:23:18 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9124#msg-9124</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9124#msg-9124</link><description><![CDATA[ Just to clarify, the code I posted before worked (not the tool the save and load functions in the post before that). However ekeeke, your functions are far better than mine and I see where I have gone wrong. Thank you for being so helpful :)]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:16:01 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9123#msg-9123</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9123#msg-9123</link><description><![CDATA[ @Arikado:as Koopa said, you have to fully understand how the fwrite/fead functions work before throwing some random code out<br /><br /><br /><blockquote class="bbcode"><div><small>Quote<br /></small><strong>http://www.cplusplus.com/reference/clibrary/cstdio/fread.html</strong><br />Parameters<br /><br />size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );<br /><br />ptr<br />Pointer to a block of memory with a minimum size of (size*count) bytes.<br />size<br />Size in bytes of each element to be read.<br />count<br />Number of elements, each one with a size of size bytes.<br />stream<br />Pointer to a FILE object that specifies an input stream.<br /></div></blockquote><br />you are experimenting crash in the last version you posted because you try to read/write outside the allocated memory (you are actually reading 30*30*4 bytes instead of 30*4 bytes) which generally lead to corrupted memory and unexpected behavior or DSI exception.<br /><br /><br />if you do something like this, for loading:<br /><br /><pre class="bbcode">FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
if (pattern)
{
  fread(&brickx[0], sizeof(brickx), 1, pattern);
  fread(&bricky[0], sizeof(bricky), 1, pattern);
  fclose(pattern);
}</pre><br />and this, for saving function:<br /><br /><pre class="bbcode">FILE *pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
if (pattern)
{
  fwrite(&brickx[0], sizeof(brickx), 1, pattern);
  fwrite(&bricky[0], sizeof(bricky), 1, pattern);
  fclose(pattern);
}</pre><br />there is absolutely no reason for you to get loaded values different from saved ones]]></description>
<dc:creator>ekeeke</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 15:11:25 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9121#msg-9121</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9121#msg-9121</link><description><![CDATA[ @Dykam<br />Yes, please. For Windows Vista. Editing is not neseccary because I only need to read the files to transfer their data into one of the modes of play. Thanks a ton in advance.<br /><br /><br />@Koopa<br />Believe it or not Koopa, I actually tried your method and I experienced loaded coordinates that were even farther off than the original coordinates than when I posted this topic.]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 14:20:03 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9110#msg-9110</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9110#msg-9110</link><description><![CDATA[ Hey Arikado,<br /><br />there are still some flaws in your loading/saving routines. Make sure you understand the meaning of the parameters to fwrite/fread.<br /><br />The parameters for fwrite are:<br />ptr<br />Pointer to the array of elements to be written, --- obviously your array brickx<br />size<br />Size in bytes of each element to be written. --- you are storing integers in the array, so size in bytes of one element is sizeof(int), or simple 4 bytes<br />count<br />Number of elements, each one with a size of size bytes. - you have 30 elements in your array<br />stream<br />Pointer to a FILE object that specifies an output stream. -- well your file descriptor: pattern<br /><br /><br />so writing should be<br />fwrite(brickx, sizeof(int), 30, pattern);<br /><br />On your notepad issue: fwrite writes binary data, so you can&#039;t view it with an ordinary text editor. You can use an hex editor to view the data.]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 11:52:29 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9106#msg-9106</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9106#msg-9106</link><description><![CDATA[ If you want a program for your pc(lin/win/mac), say it, I&#039;ll analize your code and make a reader(/editor?).]]></description>
<dc:creator>Dykam</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 07:46:21 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9089#msg-9089</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9089#msg-9089</link><description><![CDATA[ On a slightly unrelated note; When I open up the pattern file in notepad all I get is garbage characters. So I created a tool for the wii to display the values of the bricks one by one on the television screen:<br /><br /><pre class="bbcode">
int brickx[30]; //X values for the bricks
	int bricky[30]; //Y values for the bricks
	
	int brickweareon = 0;
	
	GameWindow gwd;
	
	ftImage print(640, 480);
	
	Sprite Text;
	
	FILE *pattern;
	
	void LoadPattern();//Load Pattern into level editor
	
	void LoadPattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");

	fread(brickx, 1, sizeof(brickx)*30, pattern);
	fread(bricky, 1, sizeof(bricky)*30, pattern);
	
	fclose(pattern);
	
	}

    int main(int argc, char **argv){

	
	fatInitDefault();//Initialize the front SD for loading images
	
	gwd.InitVideo();
	
	WPAD_Init(); //Initialize the Wiimote
	
	WPAD_SetDataFormat(WPAD_CHAN_0, WPAD_FMT_BTNS_ACC_IR);//Allows us to use the Wiimote pointer
	
	print.setFont(font_ttf, font_ttf_size);
	print.setSize(32);
	Text.SetPosition(100, 50);
    Text.SetImage(&print);
	print.setColor(Color::Color(0,255,255));
	
	LoadPattern();
	
	for(;;)
	{
		WPAD_ScanPads();
		
		print.printf("Brick: %d\n", brickweareon);
		print.printf("X: %d\n", brickx[brickweareon]);
		print.printf("Y: %d\n", bricky[brickweareon]);
			
		print.flush();
			
		Text.Draw();
			
		print.clear();
			
		print.reset();
		
		if(WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_A)
		brickweareon++;
		
		if((WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_BUTTON_HOME) || (WPAD_ButtonsDown(WPAD_CHAN_0)&WPAD_CLASSIC_BUTTON_HOME))
		break;	
		
		if(brickweareon &gt; 29)
		brickweareon = 0;
		
		gwd.Flush();
		
	   }
	   return 0;
     }</pre><br />Anyways, it works for about ten seconds, displaying the values of brick 0 on the screen in a nice pretty blue color. Then the wiimote unsyncs and I get a code dump. Any ideas?]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Fri, 30 Jan 2009 00:16:26 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9074#msg-9074</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9074#msg-9074</link><description><![CDATA[ Alright, I got it working. Here&#039;s the final version of the code:<br /><br /><pre class="bbcode">
                FILE *pattern;

               void SavePattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
	
	fwrite(brickx, 1, sizeof(brickx)*30, pattern);
	fwrite(bricky, 1, sizeof(bricky)*30, pattern);
	
	fclose(pattern);
	
	}
	
	void LoadPattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");

	fread(brickx, 1, sizeof(brickx)*30, pattern);
	fread(bricky, 1, sizeof(bricky)*30, pattern);
	
	fclose(pattern);
	
	}</pre><br />Thank you very much Tantric and Koopa. I couldn&#039;t have accomplished this without your help. Thanks to you, WiiBreaker now has a working, fully functional level editor. I&#039;ll be sure to mention you in the thank you section on the readme and on the wiki. Once again, thank you very much. :)]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Thu, 29 Jan 2009 20:19:31 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9057#msg-9057</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9057#msg-9057</link><description><![CDATA[ True, but platforms tends to change quite suddenly without you being prepared. It&#039;s not that unreasonable to want a level editor on the pc instead.]]></description>
<dc:creator>henke37</dc:creator>
<category>Coding</category><pubDate>Thu, 29 Jan 2009 16:38:58 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9022#msg-9022</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9022#msg-9022</link><description><![CDATA[ In my understanding you don&#039;t have to worry about it as long as you writing and reading the data on the same plattform.<br /><br />please correct me if i&#039;m wrong.]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Thu, 29 Jan 2009 12:02:48 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9021#msg-9021</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9021#msg-9021</link><description><![CDATA[ Note: watch out for endian issues when dealing with fileformats and integers!]]></description>
<dc:creator>henke37</dc:creator>
<category>Coding</category><pubDate>Thu, 29 Jan 2009 11:59:46 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,9020#msg-9020</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,9020#msg-9020</link><description><![CDATA[ When sizeof is applied to an array, the result is the size in bytes of the array in memory. The third parameter to fwrite is not the size in bytes of the array but the number of elements in the array.<br /><br />so<br /><pre class="bbcode">
fwrite(brickx, sizeof(int), 30, pattern);</pre><br /><br />will write the complete array brickx to your file. You don&#039;t need a loop for that.<br /><br /><pre class="bbcode">
fwrite(&brickx<i>, sizeof(int), 1, pattern);
</i></pre>
will write one element of the array brickx to the file. You&#039;ll want to use the for loop in this case.<br /><br />same for loading:<br /><br /><pre class="bbcode">
...
int brickx[30];
size_t elementsRead = fread(brickx, sizeof(int), 30, pattern);
...</pre><br />reads the complete array from the file.<br /><br />resp.<br /><br /><pre class="bbcode">
...
int brickx[30];
int bricky[30];

for (int i = 0; i &lt; 30; i++){
  //read one element at a time
  fread(&brickx<i>, sizeof(int), 1, pattern);
  fread(&bricky<i>, sizeof(int), 1, pattern);
....
}
...
</i></i></pre><br />Cheers,<br /><br />Koopa]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Thu, 29 Jan 2009 10:48:17 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8985#msg-8985</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8985#msg-8985</link><description><![CDATA[ Ok, great. That&#039;s looking better. But, still some mistakes.<br /><br />Why don&#039;t you just do this instead:<br /><br />in Save function:<br /><br />fwrite(brickx, 1, sizeof(brickx)*30, pattern);<br />fwrite(bricky, 1, sizeof(bricky)*30, pattern);<br /><br />in Load function:<br /><br />fread(brickx, 1, sizeof(brickx)*30, pattern);<br />fread(bricky, 1, sizeof(bricky)*30, pattern);<br /><br />Keep in mind the definitions of functions. fread returns the NUMBER of bytes read, not the VALUE.<br /><br />This line reads in the whole file, and stores the # of bytes in the file in brickx<i> (hopefully LSize)<br />brickx<i> = fread(buffer, 1, LSize, pattern);<br /><br />As for error-checking, perhaps check when you get the filesize that LSize == sizeof(brickx)*30 + sizeof(bricky)*30<br />Otherwise it&#039;s a bad file.</i></i>]]></description>
<dc:creator>Tantric</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 21:24:48 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8984#msg-8984</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8984#msg-8984</link><description><![CDATA[ Thanks. I changed my functions accordingly, but I appear to have broken loading. There are probably alot of stupid errors in it:<br /><br /><pre class="bbcode">
void SavePattern(){
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","wb");
	
	for(int i = 0; i &lt; 30; i++){
	fwrite(brickx, 1, sizeof(brickx), pattern);
	fwrite(bricky, 1, sizeof(bricky), pattern);
	}
	
	fclose(pattern);
	
	}
	
	void LoadPattern(){
	
	long LSize;
	char * buffer;
	
	pattern = fopen("sd:/apps/Wiibreaker/data/pattern1.pattern","rb");
	
	// obtain file size:
    fseek (pattern , 0 , SEEK_END);
    LSize = ftell (pattern);
    rewind (pattern);
	buffer = (char*) malloc (sizeof(char)*LSize);

	for(int i = 0; i &lt; 30; i++){
	brickx<i> = fread(buffer, 1, LSize, pattern);
	bricky<i> = fread(buffer, 1, LSize, pattern);
	}
	
	fclose(pattern);
	free(buffer);
	
	}
</i></i></pre>]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 20:57:47 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8976#msg-8976</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8976#msg-8976</link><description><![CDATA[ have a look at:<br />[<a href="http://www.cplusplus.com/reference/clibrary/cstdio/fwrite.html" rel="nofollow">www.cplusplus.com</a>]<br />resp.<br />[<a href="http://www.cplusplus.com/reference/clibrary/cstdio/fread.html" rel="nofollow">www.cplusplus.com</a>]]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 16:22:19 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8973#msg-8973</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8973#msg-8973</link><description><![CDATA[ What are the parameters for fwrite and fread?]]></description>
<dc:creator>Arikado</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 15:55:22 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8971#msg-8971</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8971#msg-8971</link><description><![CDATA[ hi,<br /><br />i was typing at the same time you did so i didn´t knew (else i wouldn´t have posted).<br />maybe arikado was getting a lead from my post.<br />but koopa´s makes even more sence :D<br /><br />i dont know anything about Wii programming just programming in general.<br />but now basicly i understand:<br />the using Char (8 bits ) as parsing medium/function is corrupting this Int ( &gt; 8 bits)<br /><br />just curious.<br /><br />warm greetingz,<br />Peter de Man]]></description>
<dc:creator>PeterdeMan</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 15:24:56 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8970#msg-8970</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8970#msg-8970</link><description><![CDATA[ Forgive me if I&#039;m wrong but am I missing something here?<br /><br />You say the values you wish to use are 0 to 640 and 0 to 480 but doesn&#039;t fputc just write 1 character and fgetc just reads 1 character?<br /><br />Edit: I agree with koopa.]]></description>
<dc:creator>teknecal</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 15:19:18 +0100</pubDate></item>
<item>
<guid>http://forum.wiibrew.org/read.php?11,8916,8969#msg-8969</guid>
<title>Re: Saving and Loading Problem</title><link>http://forum.wiibrew.org/read.php?11,8916,8969#msg-8969</link><description><![CDATA[ Using int for the coordinates is fine.<br /><br />You&#039;ll just want to use fwrite(..) instead of fputc for writing data and fread(...) instead of fgetc(..) for reading data.]]></description>
<dc:creator>koopa</dc:creator>
<category>Coding</category><pubDate>Wed, 28 Jan 2009 15:03:04 +0100</pubDate></item>
</channel>
</rss>