|
Page 1 of 1
|
[ 6 posts ] |
|
Help with file access usage
Author |
Message |
Robert Allen
Rookie
Joined: Thu Oct 28, 2010 12:23 am Posts: 2
|
 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 |
|
 |
bfeher
Site Admin
Joined: Mon Jun 08, 2009 4:50 pm Posts: 70
|
 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 |
|
 |
ultraforce.nott
Rookie
Joined: Thu Nov 20, 2014 12:12 pm Posts: 3
|
 Re: Help with file access usage
Is this code fragment compatible with the EV3 as well?
|
Mon Dec 08, 2014 1:35 pm |
|
 |
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 722
|
 Re: Help with file access usage
Per the ROBOTC for LEGO Mindstorms 4.X downloads page: 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!
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our updated help documentation and the ROBOTC Forums.
|
Mon Dec 08, 2014 2:09 pm |
|
 |
billyb29
Rookie
Joined: Sun Nov 30, 2014 4:56 am Posts: 14
|
 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?
|
Sat May 23, 2015 11:13 pm |
|
 |
alberthandsome
Rookie
Joined: Thu Jun 30, 2016 3:10 am Posts: 4
|
 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.
|
Wed Jan 11, 2017 7:44 am |
|
|
|
Page 1 of 1
|
[ 6 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
|
|