Jump to content



Photo
- - - - -

[casio C] How To Read And Write An Array To A File?


  • Please log in to reply
10 replies to this topic

#1 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 11:56 AM

hey i hope anyone here has some idea's to help me out on this little problem i have got:

i have a char array map_1[50][50]

and i would like to write it to a file and also read it back from that file. So i wrote some code i thought would work, but it does not. Is there someone out there who can help me?

#include "fxlib.h"
#include "stdio.h" 
FONTCHARACTER PathName[] = {'\\', '\\', 'f', 'l', 's', '0', '\\', 'C', 'A', 'L', 'C', 'C', 'T', 'Y', '.', 'd', 'a', 't', 0};
extern map_1[50][50];
extern k,l;
int aa;
int bb;
int cc;
char buf[21];
unsigned int key;
int file_size;

int save(){

		char save_data[3000];
		
		Bfile_DeleteFile(PathName);
		Bfile_CreateFile(PathName, 3000);
		
		for(k=0;k<=49;k++){
			for(l=0;l<=49;l++){
				sprintf(save_data, "%c",map_1[l][k]);

			}
		}
		
		
		aa = Bfile_OpenFile(PathName, _OPENMODE_WRITE);
			if (aa < 0){
				PopUpWin(2);
				PrintMini(12, 14, (unsigned char *)"Cannot Save Game", MINI_OVER);
				PrintMini(12, 21, (unsigned char *)"Probably Not Enough STOR MEM", MINI_OVER);
				Bdisp_PutDisp_DD();
				Sleep(2000);
				Bfile_CloseFile(aa);
			}
			if (aa >= 0){
				Bfile_WriteFile(aa, &save_data, 3000);
				PopUpWin(1);
				PrintMini(44, 25, (unsigned char *)"GAME SAVED", MINI_OVER);
				Bdisp_PutDisp_DD();
				Sleep(2000);
				Bfile_CloseFile(aa);
			}
}



int load(){
char game_data[3000];
			
				bb = Bfile_OpenFile(PathName, _OPENMODE_READ);
				if (bb < 0)
				{
					PopUpWin(2);
					PrintMini(12, 14, (unsigned char *)"ERROR: Cannot open file", MINI_OVER);
					PrintMini(12, 21, (unsigned char *)"No data loaded", MINI_OVER);
					Bdisp_PutDisp_DD();
					Sleep(1000);
					return 0;
				}
				file_size = Bfile_GetFileSize(bb);
				memset(game_data, 0, file_size);
				cc = Bfile_ReadFile(bb, &game_data, file_size, 0);
				if (cc >= 0)
				{
					
					PopUpWin(2);
					PrintMini(24, 14, (unsigned char *)"Saved Game Loaded", MINI_OVER);
					sprintf(buf, "%d bytes read", MINI_OVER);
					PrintMini(35, 21, (unsigned char *)buf, MINI_OVER);
					for(k=0;k<=49;k++){
						for(l=0;l<=49;l++){
							sscanf(game_data, "%s",&map_1[l][k]);
			
						}
					}


					Bdisp_PutDisp_DD();
					Sleep(2000);
					Bfile_CloseFile(bb);
				}
				if (cc < 0)
				{
					Bdisp_AllClr_VRAM();
					PopUpWin(2);
					PrintMini(12, 14, (unsigned char *)"File Is Corrupted", MINI_OVER);
					PrintMini(12, 21, (unsigned char *)".", MINI_OVER);
					Bdisp_PutDisp_DD();
					Sleep(2000);
					Bfile_CloseFile(bb);
					return 0;
					
				}
}


#2 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 17 June 2007 - 12:31 PM

May be:

#include "fxlib.h"
#include "stdio.h"
FONTCHARACTER PathName[] = {'\\', '\\', 'f', 'l', 's', '0', '\\', 'C', 'A', 'L', 'C', 'C', 'T', 'Y', '.', 'd', 'a', 't', 0};
extern map_1[50][50];
extern k,l;
int aa;
int bb;
int cc;
char buf[21];
unsigned int key;
int file_size;

int save(){
  Bfile_DeleteFile(PathName);
  Bfile_CreateFile(PathName, 3000);
  aa = Bfile_OpenFile(PathName, _OPENMODE_WRITE);
  if (aa < 0){
	//Error
	Bfile_CloseFile(aa);
  }
  if (aa >= 0){
	for(k=0;k<=49;k++){
	  for(l=0;l<=49;l++){
		Bfile_WriteFile(aa, &map_1[l][k], sizeof(char));
	  }
	}
	//...
	Bfile_CloseFile(aa);
  }
}



int load(){
  bb = Bfile_OpenFile(PathName, _OPENMODE_READ);
  if (bb < 0)
  {
	//Error
	return 0;
  }
  file_size = Bfile_GetFileSize(bb);
  for(k=0;k<=49;k++)
	  for(l=0;l<=49;l++){
	  cc = Bfile_ReadFile(bb, &map_1[l][k], sizeof(char));
	  if (cc < 0)
	  {
	  //Error
	  Bfile_CloseFile(bb);
	  return 0;
	  }
	}
}


Or:

#include "fxlib.h"
#include "stdio.h"
FONTCHARACTER PathName[] = {'\\', '\\', 'f', 'l', 's', '0', '\\', 'C', 'A', 'L', 'C', 'C', 'T', 'Y', '.', 'd', 'a', 't', 0};
extern map_1[50][50];
extern k,l;
int aa;
int bb;
int cc;
char buf[21];
unsigned int key;
int file_size;

int save(){
  Bfile_DeleteFile(PathName);
  Bfile_CreateFile(PathName, 3000);
  aa = Bfile_OpenFile(PathName, _OPENMODE_WRITE);
  if (aa < 0){
	//Error
	Bfile_CloseFile(aa);
  }
  if (aa >= 0){
	Bfile_WriteFile(aa, (unsigned char*)map_1, 50*50*sizeof(char));
	//...
	Bfile_CloseFile(aa);
  }
}



int load(){
  bb = Bfile_OpenFile(PathName, _OPENMODE_READ);
  if (bb < 0)
  {
	PopUpWin(2);
	//Error
  } else if((file_size = Bfile_GetFileSize(bb))!=50*50){
	  //Error
	  Bfile_CloseFile(aa);
  };
  Bfile_ReadFile(bb, (unsigned char*)map_1, 50*50*sizeof(char));
  Bfile_CloseFile(aa);
}


#3 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 12:36 PM

thanks for replying, i will try it now, thanks :) i have been wining all day on fora :)

#4 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 12:53 PM

nope, it doesn't work :| but thanks anyhow :)

#5 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 17 June 2007 - 01:05 PM

Doesnt work? what happned? Let me test...

#6 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 01:15 PM

ok, :) i will upload the source :) wait

last post, click here

#7 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 17 June 2007 - 01:18 PM

#include "fxlib.h"
#include "stdio.h"
FONTCHARACTER PathName[] = {'\\', '\\', 'f', 'l', 's', '0', '\\', 'C', 'A', 'L', 'C', 'C', 'T', 'Y', '.', 'd', 'a', 't', 0};
extern map_1[50][50];
extern k,l;
int aa;
int bb;
int cc;
char buf[21];
unsigned int key;
int file_size;

int save(){
Bfile_DeleteFile(PathName);
Bfile_CreateFile(PathName, 3000);
aa = Bfile_OpenFile(PathName, _OPENMODE_WRITE);
if (aa < 0){
//Error
Bfile_CloseFile(aa);
}
if (aa >= 0){
Bfile_WriteFile(aa, (unsigned char*)map_1, 50*50*sizeof(char),-1);
//...
Bfile_CloseFile(aa);
}
}



int load(){
bb = Bfile_OpenFile(PathName, _OPENMODE_READ);
if (bb < 0)
{
PopUpWin(2);
//Error
} /* WRONG HERE, THE FILE SIZE SHOULDNT BE 50*50
else if((file_size = Bfile_GetFileSize(bb))!=50*50){
//Error
Bfile_CloseFile(aa);
};*/

Bfile_ReadFile(bb, (unsigned char*)map_1, 50*50*sizeof(char));
Bfile_CloseFile(aa);
}

#8 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 01:21 PM

Executing Hitachi SH C/C++ Compiler/Assembler phase

set SHC_INC=C:\Program Files\CASIO\fx-9860G SDK\OS\SH\include
set PATH=C:\Program Files\CASIO\fx-9860G SDK\OS\SH\bin
set SHC_LIB=C:\Program Files\CASIO\fx-9860G SDK\OS\SH\bin
set SHC_TMP=C:\Documents and Settings\Aspire1511\Bureaublad\RTS\Debug
"C:\Program Files\CASIO\fx-9860G SDK\OS\SH\bin\shc.exe" -subcommand=C:\DOCUME~1\ASPIRE~1\LOCALS~1\Temp\hmk12B.tmp
C:\Documents and Settings\Aspire1511\Bureaublad\RTS\fileop.c(41) : C2202 (E) Number of parameters mismatch

HMAKE MAKE UTILITY Ver. 1.1
Copyright (C) Hitachi Micro Systems Europe Ltd. 1998
Copyright (C) Hitachi Ltd. 1998 


	ERROR: Process failed with return code: 1
Build was not successful.

it will not compile

C:\Documents and Settings\Aspire1511\Bureaublad\RTS\fileop.c(41) : C2202 (E) Number of parameters mismatch

is

Bfile_ReadFile(bb, (unsigned char*)map_1, 50*50*sizeof(char));


#9 vanhoa

vanhoa

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 854 posts
  • Gender:Male
  • Location:Vietnam

  • Calculators:
    AFX 2.0, CP 300, CP 330, nSpire, TI 89, FX 5800

Posted 17 June 2007 - 01:29 PM

Bfile_ReadFile(bb, (unsigned char*)map_1, 50*50*sizeof(char),-1);

#10 Menno

Menno

    Casio Freak

  • Members
  • PipPipPipPip
  • 184 posts
  • Gender:Male
  • Location:Netherlands

  • Calculators:
    Casio 880P
    Casio Graph 25+
    Casio fx-9860g sd

Posted 17 June 2007 - 01:34 PM

Bfile_ReadFile(bb, (unsigned char*)map_1, 50*50*sizeof(char),-1);





:roflol: :roflol: :roflol: :roflol: :roflol: :roflol: :roflol: :roflol: :roflol: :roflol: :roflol:


it works

it works

it works

yoho yohee yoho

VanHoa is the Hero (i will mention you in the credits of the game :) )

#11 oxinabox

oxinabox

    Newbie

  • Members
  • Pip
  • 21 posts

  • Calculators:
    fx-9860 G AU

Posted 18 June 2007 - 11:24 AM

Now, who's going to teach me...

Thanks vanhoa, Why don't you come back to CK?
Maybe things calm down...


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users