View unanswered posts | View active topics It is currently Tue Jul 29, 2014 7:59 am






Reply to topic  [ 2 posts ] 
Help with file access usage 
Author Message
Rookie

Joined: Thu Oct 28, 2010 12:23 am
Posts: 2
Post 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


Sat Dec 03, 2011 9:17 pm
Profile
Site Admin
Site Admin

Joined: Mon Jun 08, 2009 4:50 pm
Posts: 70
Post 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
}

_________________
Bence Feher

Undergraduate Intern - NREC, Robotics Academy
ROBOTC - Testing/Documentation/Developer

Computer Science, Japanese, East Asian Studies
University of Pittsburgh, Konan University 甲南大学


Mon Dec 05, 2011 12:58 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.