Page 1 of 1

[SOLVED]NXC Read/Write Help

Posted: 09 Nov 2012, 23:54
by jld001
Here is my code: https://github.com/ArtskydJ/NXC_Tests/b ... String.nxc

Code: Select all

//Read/Write String Test

task main()
  {
  int what=0;
  unsigned int handle,fsize=1024,result;
  string outstring;
  TextOut(2,0,"make|delete|open",0);
  until (ButtonPressed(BTNLEFT)||ButtonPressed(BTNCENTER)||ButtonPressed(BTNRIGHT)) {}
  if (ButtonPressed(BTNLEFT))   {what=0;}
  if (ButtonPressed(BTNCENTER)) {what=1;}
  if (ButtonPressed(BTNRIGHT))  {what=2;}
  if (what==0)
    {
    TextOut(0,48,"Writing",0);
    result=CreateFile("monkey.txt",1024,handle); //change to .dat later
    if (result==LDR_SUCCESS)
      {
      TextOut(0,40,"Written!",0);
      Write(handle,"Joe is not a monkey...");
      Write(handle,"Joe is a human.");
      CloseFile(handle);
      PlayTone(500,500);
      Wait(500);
      }
    else if (result==LDR_FILEEXISTS)
      {
      TextOut(0,56,"FILE EXISTS",0);
      SendResponseNumber(MAILBOX10,result);
      }
    else
      {
      TextOut(0,56,"UNKNOWN ERROR",0);
      SendResponseNumber(MAILBOX10,result);
      }
    }
  else if (what==1)
    {
    Wait(100);
    TextOut(0,48,"Deleting",0);
    result=DeleteFile("monkey.txt");
    if (result==LDR_SUCCESS)
      {TextOut(0,40,"Deleted!",0);}
    else if (result=LDR_FILEISBUSY)
      {
      TextOut(0,56,"FILE IS BUSY",0);
      SendResponseNumber(MAILBOX10,result);
      }
    else
      {
      TextOut(0,56,"UNKNOWN ERROR",0);
      SendResponseNumber(MAILBOX10,result);
      }
    }
  else if (what==2)
    {
    TextOut(0,48,"Reading",0);
    result=OpenFileRead("monkey.txt",fsize,handle);
    if (result==LDR_SUCCESS)
      {
      TextOut(0,40,"Read!",0);
      Read(handle,outstring);
      CloseFile(handle);
      SendResponseString(MAILBOX10,outstring);
      TextOut(0,24,outstring,0);
      PlayTone(2000,500);
      Wait(500);
      }
    else if (result==LDR_FILENOTFOUND)
      {
      TextOut(0,56,"FILE NOT FOUND",0);
      SendResponseNumber(MAILBOX10,result);
      }
    else
      {
      TextOut(0,56,"UNKNOWN ERROR",0);
      SendResponseNumber(MAILBOX10,result);
      }
    }
  Wait(1000);
  }
For some reason I do not see anything when trying to view after I create the file. Help?

EDIT:
BTW, the text file reads fine on the computer. I opened it in notepad++, and it showed the exact strings.
Why does it not read on the NXT?
Thanks in advance!
EDIT 2:
Also, when opened in notepad++, why are there 2 null terminators?

Re: NXC Read/Write Help

Posted: 10 Nov 2012, 15:15
by HaWe
hi,
I have no idea what's going wrong with your code. I seldom write or read files, but if, then I'm using the following dummy file i/o commands:
(instead of int use string mydata1,...):

Code: Select all

// File I/O

void ReadFile() {
  string sFileName="myfile.dat";
  unsigned int nFileSize;
  byte fHandle;
  int IOresult;
  int mydata1, mydata2, mydata3, mydata4; // dummies


  IOresult = OpenFileRead(sFileName, nFileSize, fHandle);
  if (IOresult == LDR_SUCCESS) {

    ReadLn (fHandle, mydata1);
    ReadLn (fHandle, mydata2);
    ReadLn (fHandle, mydata3);
    ReadLn (fHandle, mydata4);  // ...oder Müller ...

    CloseFile(fHandle);
    TextOut(0,0,"FILE READ OK!");

  }
  else   {
    TextOut(0,0,"FILE ERROR ! ");
  }

}


void WriteFile() {
  string sFileName = "myfile.dat";
  unsigned int nFileSize = 256; // estimated file zize
  byte fHandle;
  int IOresult;
  int mydata1, mydata2, mydata3, mydata4;  // dummies

  DeleteFile(sFileName);
  IOresult = CreateFile(sFileName, nFileSize, fHandle);
  if (IOresult == LDR_SUCCESS) {

    WriteLn (fHandle, mydata1);
    WriteLn (fHandle, mydata2);
    WriteLn (fHandle, mydata3);
    WriteLn (fHandle, mydata4);   // ...oder was...

    CloseFile(fHandle);
    TextOut(0,0, "FILE WRITE OK!  ");
  }
  else  {
    TextOut(0,0, "FILE WRITE ERROR!");
  }
}
maybe that might help you.

Re: NXC Read/Write Help

Posted: 10 Nov 2012, 16:20
by jld001
I modified your code, and I found out that the nxt is ok with numbers, but does not like strings.

Code: Select all

// File I/O

void ReadFile()
  {
  string sFileName="myfile.dat";
  unsigned int nFileSize;
  byte fHandle;
  int IOresult;
  int mydata1, mydata2, mydata3, mydata4, mydata5, mydata6;
  /*string mydata1, mydata2, mydata3, mydata4, mydata5, mydata6;*/

  IOresult = OpenFileRead(sFileName, nFileSize, fHandle);
  if (IOresult == LDR_SUCCESS)
    {
    ReadLn (fHandle, mydata1);
    ReadLn (fHandle, mydata2);
    ReadLn (fHandle, mydata3);
    ReadLn (fHandle, mydata4);
    ReadLn (fHandle, mydata5);
    ReadLn (fHandle, mydata6);

    CloseFile(fHandle);
    TextOut(0,0,"FILE READ OK!");

    NumOut(0,56,mydata1,0);
    NumOut(0,48,mydata2,0);
    NumOut(0,40,mydata3,0);
    NumOut(0,32,mydata4,0);
    NumOut(0,24,mydata5,0);
    NumOut(0,16,mydata6,0);
    /*
    TextOut(0,56,mydata1,0);
    TextOut(0,48,mydata2,0);
    TextOut(0,40,mydata3,0);
    TextOut(0,32,mydata4,0);
    TextOut(0,24,mydata5,0);
    TextOut(0,16,mydata6,0);
    */
    while(1) {}
    }
  else {TextOut(0,0,"FILE ERROR ! ");}
  }


void WriteFile()
  {
  string sFileName = "myfile.dat";
  unsigned int nFileSize = 1024; // estimated file zize
  byte fHandle;
  int IOresult;
  int mydata1=4, mydata2=8, mydata3=15, mydata4=16, mydata5=23, mydata6=42;  //lost :)
  IOresult = CreateFile(sFileName, nFileSize, fHandle);
  if (IOresult==LDR_FILEEXISTS)
    {
    DeleteFile(sFileName);
    IOresult = CreateFile(sFileName, nFileSize, fHandle);
    }
  if (IOresult == LDR_SUCCESS)
    {
    WriteLn (fHandle, mydata1);
    WriteLn (fHandle, mydata2);
    WriteLn (fHandle, mydata3);
    WriteLn (fHandle, mydata4);
    WriteLn (fHandle, mydata5);
    WriteLn (fHandle, mydata6);

    CloseFile(fHandle);
    TextOut(0,0, "FILE WRITE OK!  ");

    Wait(1000);
    }
  else {TextOut(0,0, "FILE WRITE ERROR!");}
  }



task main()
  {
  WriteFile();
  ReadFile();
  }
EDIT:
Maybe I'll write the letter's byte representations to a file and read the file and convert it back into a string. :)

Re: NXC Read/Write Help

Posted: 10 Nov 2012, 16:42
by HaWe
hmm - I never used strings as data before.

...but maybe you try to represent a string by an array of char?
(maybe that's an issue about the " \0 termination" of strings vs. char arrays)

Re: NXC Read/Write Help

Posted: 10 Nov 2012, 16:52
by afanofosc
I would use the API functions designed for writing strings to files if I needed to write strings to a file such as WriteString or WriteLnString.

John Hansen

Re: NXC Read/Write Help

Posted: 10 Nov 2012, 19:25
by HaWe
brilliant idea -
and I also stupidly completely forgot about
fputs, fprintf, and fgets :mrgreen:

Re: NXC Read/Write Help

Posted: 15 Nov 2012, 04:17
by jld001
afanofosc wrote:I would use the API functions designed for writing strings to files if I needed to write strings to a file such as WriteString or WriteLnString.

John Hansen
Thank you!!! It's working now! :)