Page 1 of 2

NXC Reading and writing arrays.

Posted: 27 Nov 2010, 16:45
by nxtboyiii
I've found that there is a limit to how much is written into a file.
In this code, it shows that it won't read and write correctly because "map" is too large.

Code: Select all

LocationType map[]={{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3}};
int A1[];
int A2[];
int fs=2000;
long handle=0;
long handle2=0;
task main()
{
while(1)
{
if(ButtonPressed(BTNLEFT,0))
{
// string text=ByteArrayToStr(map);
//TextOut(0,0,text);
// Wait(5000);
ClearScreen();
long Arrlen=ArrayLen(map);
      ArrayInit(A1,0,Arrlen);
     ArrayInit(A2,0,Arrlen);
      for(long c=0; c < Arrlen;c++)
     {
      A1[c]=map[c].X;
     }
     PlayToneEx(500,30,1,0);
          for(long c=0; c < Arrlen;c++)
     {
      A2[c]=map[c].Y;
     }
DeleteFile("test.tst");
CreateFile("test.tst",fs,handle);
Write(handle,A1);
CloseFile(handle);
  DeleteFile("test2.tst");
CreateFile("test2.tst",fs,handle2);
Write(handle2,A2);
CloseFile(handle2);
PlayToneEx(400,30,1,0);
}
if(ButtonPressed(BTNRIGHT,0))
{
//string text2;
OpenFileRead("test.tst",fs,handle);
Read(handle,A1);
CloseFile(handle);
  OpenFileRead("test2.tst",fs,handle2);
Read(handle2,A2);
CloseFile(handle);
  long Arrlen=ArrayLen(A1);
         for(long c=0; c < Arrlen;c++)
     {
      map[c].X=A1[c];
     }
          for(long c=0; c < Arrlen;c++)
     {
      map[c].Y=A2[c];
     }
//StrToByteArray(text2,map);
NumOut(0,0,Arrlen);
Wait(1000);
ClearScreen();
}
}
}
Though in this code, it works fine:

Code: Select all

LocationType map[]={{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1}};
int A1[];
int A2[];
int fs=2000;
long handle=0;
long handle2=0;
task main()
{
while(1)
{
if(ButtonPressed(BTNLEFT,0))
{
// string text=ByteArrayToStr(map);
//TextOut(0,0,text);
// Wait(5000);
ClearScreen();
long Arrlen=ArrayLen(map);
      ArrayInit(A1,0,Arrlen);
     ArrayInit(A2,0,Arrlen);
      for(long c=0; c < Arrlen;c++)
     {
      A1[c]=map[c].X;
     }
     PlayToneEx(500,30,1,0);
          for(long c=0; c < Arrlen;c++)
     {
      A2[c]=map[c].Y;
     }
DeleteFile("test.tst");
CreateFile("test.tst",fs,handle);
Write(handle,A1);
CloseFile(handle);
  DeleteFile("test2.tst");
CreateFile("test2.tst",fs,handle2);
Write(handle2,A2);
CloseFile(handle2);
PlayToneEx(400,30,1,0);
}
if(ButtonPressed(BTNRIGHT,0))
{
//string text2;
OpenFileRead("test.tst",fs,handle);
Read(handle,A1);
CloseFile(handle);
  OpenFileRead("test2.tst",fs,handle2);
Read(handle2,A2);
CloseFile(handle);
  long Arrlen=ArrayLen(A1);
         for(long c=0; c < Arrlen;c++)
     {
      map[c].X=A1[c];
     }
          for(long c=0; c < Arrlen;c++)
     {
      map[c].Y=A2[c];
     }
//StrToByteArray(text2,map);
NumOut(0,0,Arrlen);
Wait(1000);
ClearScreen();
}
}
}
Is there a way to read and write the file so it works, even though it has a large array?

Thanks, and have a nice day,
nxtboy III

Re: NXC Reading and writing arrays.

Posted: 28 Nov 2010, 02:07
by m-goldberg
With a few minor changes your program compiled and I was able to run it on my NXT. It created two files 'test.tst' and 'test2.tst' and played sounds and wrote 117 to the bottom of the NXT display. Here is the code I compiled:

Code: Select all

LocationType map[] = {{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3}};

int A1[];
int A2[];
int fs; // change
byte handle = 0; // change
byte handle2 = 0; // change

task main()
{
   while(1)
   {
      fs = ArrayLen(map) * 4;  // change
      if(ButtonPressed(BTNLEFT, 0))
      {
         ClearScreen();
         int Arrlen = ArrayLen(map);
         ArrayInit(A1, 0, Arrlen);
         ArrayInit(A2, 0, Arrlen);
         for(int c=0; c < Arrlen; c++)
         {
            A1[c]=map[c].X;
         }
         PlayToneEx(500, 30, 1, 0);
         for(int c=0; c < Arrlen; c++)
         {
            A2[c]=map[c].Y;
         }
         DeleteFile("test.tst");
         CreateFile("test.tst", fs, handle);
         Write(handle, A1);
         CloseFile(handle);
         DeleteFile("test2.tst");
         CreateFile("test2.tst", fs, handle2);
         Write(handle2, A2);
         CloseFile(handle2);
         PlayToneEx(400, 30, 1, 0);
      }
      if(ButtonPressed(BTNRIGHT, 0))
      {
         OpenFileRead("test.tst", fs, handle);
         Read(handle, A1);
         CloseFile(handle);
         OpenFileRead("test2.tst", fs, handle2);
         Read(handle2, A2);
         CloseFile(handle);
         int Arrlen=ArrayLen(A1);
         for(int c=0; c < Arrlen; c++)
         {
            map[c].X=A1[c];
         }
         for(int c=0; c < Arrlen; c++)
         {
            map[c].Y=A2[c];
         }
         NumOut(0, 0, Arrlen);
         Wait(1000);
         ClearScreen();
      }
   }
}

Re: NXC Reading and writing arrays.

Posted: 28 Nov 2010, 11:00
by HaWe
what is the reason why the changes work? what was the definite bug?

Re: NXC Reading and writing arrays.

Posted: 28 Nov 2010, 19:12
by m-goldberg
All of the changes I made improve the code, but it's possible that none of them actually fix the problem the OP is experiencing. It may work on my NXT because I have a lot more free memory than he does and have the space needed to allocate the files while he doesn't. My NXT has enough free space that the program might have worked even if I didn't compute the file space needed but kept the OP's huge 4000 byte allocation.

However, my version has a better chance of running on the OP's NXT because the statement fs = ArrayLen(map) * 4; cuts down the file size from 2000 bytes to 468 bytes. With two files allocated, that saves over 3000 bytes.

Perhaps nxtboyiii will be able to settle the matter. Only he knows how much free memory his NXT has for file storage.

Re: NXC Reading and writing arrays.

Posted: 29 Nov 2010, 02:37
by nxtboyiii
But what does the NXT display when you press the left button, close out of the program on NXT, start it again, and press the right button? On mine it displays 0.

Re: NXC Reading and writing arrays.

Posted: 29 Nov 2010, 18:46
by m-goldberg
You get the behavior you observe when you restart the program because you don't initialize the arrays A1 and A2 the second time around. The following code works even if you only push the left button the first time and right button the second time. It also has the advantage of not reading/writing the files many times while you are pushing the buttons (it waits until you release a button before it goes on).

Code: Select all

LocationType map[] = {{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3}};

int A1[];
int A2[];
int fs;
byte handle = 0;
byte handle2 = 0;

task main()
{
   int Arrlen = ArrayLen(map);
   ArrayInit(A1, 0, Arrlen);
   ArrayInit(A2, 0, Arrlen);
   fs = Arrlen * 4;
   while(1)
   {
      if(ButtonPressed(BTNLEFT, 0))
      {
         ClearScreen();
         for(int c=0; c < Arrlen; c++)
         {
            A1[c]=map[c].X;
         }
         PlayToneEx(500, 30, 1, 0);
         for(int c=0; c < Arrlen; c++)
         {
            A2[c]=map[c].Y;
         }
         DeleteFile("test.tst");
         CreateFile("test.tst", fs, handle);
         Write(handle, A1);
         CloseFile(handle);
         DeleteFile("test2.tst");
         CreateFile("test2.tst", fs, handle2);
         Write(handle2, A2);
         CloseFile(handle2);
         PlayTone(TONE_A4, MS_500);
         while(ButtonPressed(BTNLEFT, 0));
      }
      if(ButtonPressed(BTNRIGHT, 0))
      {
         ClearScreen();
         OpenFileRead("test.tst", fs, handle);
         Read(handle, A1);
         CloseFile(handle);
         OpenFileRead("test2.tst", fs, handle2);
         Read(handle2, A2);
         CloseFile(handle);
         int Arrlen=ArrayLen(A1);
         for(int c=0; c < Arrlen; c++)
         {
            map[c].X=A1[c];
         }
         for(int c=0; c < Arrlen; c++)
         {
            map[c].Y=A2[c];
         }
         NumOut(0, 0, Arrlen);
         PlayTone(TONE_A6, MS_500);
         while(ButtonPressed(BTNRIGHT, 0));
      }
   }
}

Re: NXC Reading and writing arrays.

Posted: 29 Nov 2010, 21:54
by nxtboyiii
But it still doesn't read it correctly. :(
Is there a way to fix it?

Re: NXC Reading and writing arrays.

Posted: 30 Nov 2010, 00:18
by m-goldberg
But it still doesn't read it correctly.
Is there a way to fix it?
What doesn't read what correctly? My ESP isn't working today; I can't read your mind. Can you give the complete details on what is going wrong and how you want it fixed?

Edit: ignore this. My ESP turned on suddenly this evening.

Re: NXC Reading and writing arrays.

Posted: 30 Nov 2010, 03:00
by m-goldberg
I have determined that Read(handle, var) doesn't work when var is an array identifier. This surprises me. It means that the values in the files weren't being read into A1 and A2. There is a work-around for this -- you can read the integer values in the files using Read by reading them one by one. Here is some code demonstrating the work-around:

Code: Select all

LocationType map[] = {{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3},{1,1},{2,2},{3,3}};

int A1[];
int A2[];
int fs;
byte handle = 0;
byte handle2 = 0;

task main()
{
   int Arrlen = ArrayLen(map);
   ArrayInit(A1, 0, Arrlen);
   ArrayInit(A2, 0, Arrlen);
   fs = Arrlen * 2;
   while(true)
   {
      if(ButtonPressed(BTNLEFT, 0))
      {
         ClearScreen();
         for(int c=0; c < Arrlen; c++)
         {
            A1[c] = map[c].X;
         }
         PlayToneEx(500, 30, 1, 0);
         for(int c=0; c < Arrlen; c++)
         {
            A2[c]=  map[c].Y;
         }
         DeleteFile("test.tst");
         CreateFile("test.tst", fs, handle);
         Write(handle, A1);
         CloseFile(handle);
         DeleteFile("test2.tst");
         CreateFile("test2.tst", fs, handle2);
         Write(handle2, A2);
         CloseFile(handle2);
         PlayTone(TONE_A4, MS_500);
         while(ButtonPressed(BTNLEFT, 0));
      }
      if(ButtonPressed(BTNRIGHT, 0))
      {
         ClearScreen();
         unsigned int item;
         A1[1] = 42, A2[1] = 42;
         OpenFileRead("test.tst", fs, handle);
         for (int i = 0; i < Arrlen; ++i)
         {
            Read(handle, item);
            A1[i] = item;
         }
         CloseFile(handle);
         OpenFileRead("test2.tst", fs, handle2);
         for (int i = 0; i < Arrlen; ++i)
         {
            Read(handle, item);
            A2[i] = item;
         }
         CloseFile(handle);
         Arrlen=ArrayLen(A1);
         for(int c=0; c < Arrlen; c++)
         {
            map[c].X = A1[c];
         }
         for(int c=0; c < Arrlen; c++)
         {
            map[c].Y = A2[c];
         }
         NumOut(0, LCD_LINE6, A1[1]);
         NumOut(10, LCD_LINE6, A2[1]);
         NumOut(0, LCD_LINE7, map[1].X);
         NumOut(10, LCD_LINE7, map[1].Y);
         NumOut(0, LCD_LINE8, Arrlen);
         PlayTone(TONE_A6, MS_500);
         while(ButtonPressed(BTNRIGHT, 0));
      }
   }
}
I hope John Hansen will comment on this. John, should Read be able to read in an array of ints with a single call?

Re: NXC Reading and writing arrays.

Posted: 30 Nov 2010, 03:21
by nxtboyiii
Thank you so much!!
Now it works!!
:) :) :)