ROBOTC.net forums
http://www.robotc.net/forums/

Help with file access usage
http://www.robotc.net/forums/viewtopic.php?f=52&t=3824
Page 1 of 1

Author:  Robert Allen [ Sat Dec 03, 2011 9:17 pm ]
Post subject:  Help with file access usage

Hello,

Last year, my school's FTC robotics team had trouble making an autonomous program, and so we didn't get as many points as we wanted to. This year, I am trying to implement the autonomous section of the match into the robot's program. I thought that it would be a good idea to have a pre-match configuration program that would create a file that the autonomous program could then read before the waitForStart() function. I would like some help with this due to the fact that the RobotC help file doesn't really say all that much (no examples) about the file access section of the nxt functions.

Robert Allen, Glencoe High School

Author:  bfeher [ Mon Dec 05, 2011 12:58 pm ]
Post subject:  Re: Help with file access usage

Hi Robert Allen,

Below is a small sample for reading and writing strings and bytes. If you are working with numbers but like to read in chars you can always store your numbers as a string and then convert them back to ints and floats after reading them with atoi("3.14") and atof("3.14"). Anyways, have a look at the code below and feel free to respond with any questions. We are currently working on an API revamp that we hope everybody will find much more useful than our current documentation.

Code:
task main()
{
  TFileHandle   hFileHandle;              // will keep track of our file
  TFileIOResult nIOResult;                // will store our IO results
  string        sFileName = "test.txt";   // the name of our file
  int           nFileSize = 100;          // will store our file size
 
  byte CR = 0x13;   // define CR (carriage return)
  byte LF = 0x10;   // define LF (line feed)
 
  string        sMessageToWrite = "ROBOTC IO test!";    // we will write this to the file
  string        sMesageToWrite_2 = "A new line!";       // we will also write this to the file on the next line
  char          incommingChar;                          // this will store each char as we read back in from the file
  string        incommingString[5];                     // this will store the final, fully-read strings (with new strings getting put into new indexes
  int           nLineCounter = 0;                       // this will let us know which line we are on when reading and writing (used as the index to 'incommingString[]')
 
  Delete("test.txt",nIOResult);
  OpenWrite(hFileHandle, nIOResult, sFileName, nFileSize);    // open the file for writing (creates the file if it does not exist)
  WriteText(hFileHandle, nIOResult, sMessageToWrite);         // write 'sMessageToWrite' to the file
  WriteByte(hFileHandle, nIOResult, CR);                      // write 'CR' to the file (carriage return)
  WriteByte(hFileHandle, nIOResult, LF);                      // write 'LF' to the file (line feed)
  WriteText(hFileHandle, nIOResult, sMesageToWrite_2);        // write 'sMesageToWrite_2' to the file
  Close(hFileHandle, nIOResult);                              // close the file (DON'T FORGET THIS STEP!)

  wait1Msec(1000);  // just a wait so we can see the variables updating in the ROBOTC debugger in order (this is not necessary)
 
  OpenRead(hFileHandle, nIOResult, sFileName, nFileSize);     // open our file 'sFileName' for reading
 
  for(int i = 0; i < nFileSize; i++)                          // iterate through the file until we've hit the end:
  {
    ReadByte(hFileHandle, nIOResult, incommingChar);            // read in a single byte
   
    if(incommingChar == CR || incommingChar == LF)              // if the incomming byte is a carriage return or a line feed:
    {
      if(incommingChar == LF)                                     // if it's a line feed:
        nLineCounter++;                                             // increment our index (will now store next char on a 'new line' (a new index into our 'incommingString[]')
    }
    else
    {
      incommingString[nLineCounter] += incommingChar;             // append that byte (char) to the end of our final string, at the right index
    }
  }
  Close(hFileHandle, nIOResult);                              // close our file (DON'T FORGET THIS STEP!)
 
  for(int i = 1; i <= 5; i++)
  {
    nxtDisplayCenteredTextLine(i, incommingString[i-1]);
  }
 
 
  while(true);  // infinite loop to keep our program running so that we can view the ROBOTC debugger variables
}

Author:  ultraforce.nott [ Mon Dec 08, 2014 1:35 pm ]
Post subject:  Re: Help with file access usage

Is this code fragment compatible with the EV3 as well?

Author:  JohnWatson [ Mon Dec 08, 2014 2:09 pm ]
Post subject:  Re: Help with file access usage

Per the ROBOTC for LEGO Mindstorms 4.X downloads page:

Quote:
Functionality Missing/Still in Development:

EV3 External Controller (TETRIX, MATRIX) Support
Daisy Chaining EV3's together and using chained motors/sensors
Wifi Download and Debugging
Joystick Support for EV3
File System Commands (Read/Write files in a ROBOTC program)


As of right now, file system commands are not fully compatible with the EV3 platform. It is on our list of things to develop, however, and we hope to have a build with this functionality available as soon as possible.

Thank you!

Author:  billyb29 [ Sat May 23, 2015 11:13 pm ]
Post subject:  Re: Help with file access usage

Hello there,

Well I was playing around with that code sample, and when I tried to compile it it gave me these 2 errors:

**Error**:Procedure call Parameters don't match declaration for 'OpenWrite(TFileHandle & hFileHandle, TFileIOResult & nIoResult, char * pzFileName, short & nFileSize)'
**Error**:Procedure call Parameters don't match declaration for 'OpenRead(TFileHandle & hFileHandle, TFileIOResult & nIoResult, char * pzFileName, short & nFileSize)'

I fixed it by changing thet type of the nFileSize variable to be short, but is there any other way to overcome this problem and make it work with int as well as short?

Author:  alberthandsome [ Wed Jan 11, 2017 7:44 am ]
Post subject:  Re: Help with file access usage

I have a friend who owns some websites, one is an info site and two are forums and he got a message from the host that it's getting too big in CPU usage and there is a problem that it could be shut down. He has been working very hard on these sites and has been paying for them out of his own pocket but he really can't afford to pay any more then he already is. Many people use his sites and it would be very disappointing if it was to go down.
He was told by the hosts that if they moved to a RootDS Server they would be able to keep the sites but the problem is that the cost would be much more.
I was just wondering if any of you had any solutions or any suggestions that would help out with the amount of usuage being used. People are already willing to donate some, but I don't think that will be enough. We are guide to u.

Android Training in Chennai | Android Training in Chennai | Android Training in Chennai
Any help would be appreciated.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/