[SOLVED]NXC Read/Write Help

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
jld001
Posts: 19
Joined: 07 Sep 2012, 21:32

[SOLVED]NXC Read/Write Help

Post 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?
Last edited by jld001 on 16 Nov 2012, 16:51, edited 1 time in total.
My Githubz of Source Codez, in order of awesomeness:
https://github.com/ArtskydJ/NXC_Source
https://github.com/ArtskydJ/NXC_Tests
My Lego Creations, in no particular order:
LEGO Bop-It - http://www.youtube.com/watch?v=6TsqrBVKbj0
LEGO Safe - http://www.youtube.com/watch?v=MyDRfVquHuY
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Read/Write Help

Post 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.
jld001
Posts: 19
Joined: 07 Sep 2012, 21:32

Re: NXC Read/Write Help

Post 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. :)
My Githubz of Source Codez, in order of awesomeness:
https://github.com/ArtskydJ/NXC_Source
https://github.com/ArtskydJ/NXC_Tests
My Lego Creations, in no particular order:
LEGO Bop-It - http://www.youtube.com/watch?v=6TsqrBVKbj0
LEGO Safe - http://www.youtube.com/watch?v=MyDRfVquHuY
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Read/Write Help

Post 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)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC Read/Write Help

Post 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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC Read/Write Help

Post by HaWe »

brilliant idea -
and I also stupidly completely forgot about
fputs, fprintf, and fgets :mrgreen:
jld001
Posts: 19
Joined: 07 Sep 2012, 21:32

Re: NXC Read/Write Help

Post 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! :)
My Githubz of Source Codez, in order of awesomeness:
https://github.com/ArtskydJ/NXC_Source
https://github.com/ArtskydJ/NXC_Tests
My Lego Creations, in no particular order:
LEGO Bop-It - http://www.youtube.com/watch?v=6TsqrBVKbj0
LEGO Safe - http://www.youtube.com/watch?v=MyDRfVquHuY
Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests