Page 1 of 4
NXC Questions
Posted: 14 Feb 2011, 19:13
by borntoown
*I really want to program in this language and sorry if I ask alot
*
**I will keep asking questions in this topic so stay tuned
**
OK:
1) What should I write to make a:
A) Conditional loop (If something happened, loop finishes).
ANSWERED
B) To know the existence of a file (If YES, skip a part of the program. If NO, go to that part, which might be the creation of the file)
ANSWERED.
2) I should close a file first in order to read it later (Like NXT-G)?
ANSWERED
3) I asked this before but, if you please, explain the NXT file system (This is important to me)
ANSWERED.
4) Why, when i start writing a program, i feel like a newbie?! I know most of the commands but there seems to be anthor problem?! What should i do?
ANSWERED
ALL QUESTIONS ARE ANSWERED, THANKS.
No more questions for now.
Thanks.
Re: NXC Questions
Posted: 15 Feb 2011, 00:36
by mattallen37
1.A To make a loop that exits under certain circumstances, you can use a break; command. You could instead, have the loop in a function (void), and use the return; command to go back to the task that called the function.
2 IIRC, you should close it when you are done with it for the entire program. Basically, you should only close it once, just before the program ends (or once you know you are done using it).
4 You mean, a feeling of not knowing where to start? You have a lot of ideas, but you have a hard time typing them into code? For me, it helped to just practice a lot (make a lot of programs that were sort of functionally pointless). I first have to think of a flow that I want (sequence of events), then I make the tasks/functions do accomplish them, and then I "string" them together to form the proper sequence.
Re: NXC Questions
Posted: 15 Feb 2011, 02:40
by muntoo
mattallen37 wrote:4 You mean, a feeling of not knowing where to start? You have a lot of ideas, but you have a hard time typing them into code? For me, it helped to just practice a lot (make a lot of programs that were sort of functionally pointless). I first have to think of a flow that I want (sequence of events), then I make the tasks/functions do accomplish them, and then I "string" them together to form the proper sequence.
Think*. Plan. Design. Build. Test. Build. Test. Build. Test. (1000 "Build. Test."s later...) Test. Test. Test. Publish.
* Of course you think the entire time... I hope.
Plan - In your head.
Design - Write it down. (On paper?)
Building - It depends on your style. Top-Down/Bottom-Up/Modular/OO7**/etc...
** James Bond does OO7 programming.
Re: NXC Questions
Posted: 15 Feb 2011, 07:44
by borntoown
First, thank you.
mattallen37 wrote:1.A To make a loop that exits under certain circumstances, you can use a break; command. You could instead, have the loop in a function (void), and use the return; command to go back to the task that called the function.
But break; command doesn't show a condition (like pressing a touch sensor), or should i use it like this?
Code: Select all
int x = 10;
task main () {
SetSensorTouch(S1);
while (x = 10)
{
NumOut(8,32,"x = 10");
if (Sensor(S1) == 1) {
break; // look here
}
}
}
Re: NXC Questions
Posted: 15 Feb 2011, 08:25
by mattallen37
Indeed, you can use it with an if statement.
Re: NXC Questions
Posted: 16 Feb 2011, 02:21
by muntoo
break is used to break out of while and for loops, and switch statements. It has no effect on any other control structures (not that I know of). This might change in C++0x, when foreach may be introduced, but you don't need to worry about that; it's just while, for, and switch for now.
Re: NXC Questions
Posted: 25 Feb 2011, 11:46
by borntoown
Thanks.
Please answer the rest of the questions
Re: NXC Questions
Posted: 25 Feb 2011, 21:26
by timpattinson
There may be a better way to find if a file exists but if not try this...
It will return 1(same as true) if the file exists, 0 (same as false) if if doesn't and -1 if there was an error
Code: Select all
int IfFileExists(string fname)
{
int fsize;
byte handle;
if(OpenFileRead(fname,fsize, handle) == LDR_SUCCESS) // All loader module functions return error codes
{
//File exists
CloseFile(handle);
return 1;
}
else if(OpenFileRead(fname,fsize, handle) == LDR_FILENOTFOUND)
{
// File does not exist
return 0;
}
else
{
//Other error
return -1;
}
}
Code: Select all
if(IfFileExists("myfile.txt"") == 1)
{
//do something if it exists
}
else
{
//do something if it doesn't
}
Good luck...
-Tim
Re: NXC Questions
Posted: 25 Feb 2011, 21:35
by HaWe
OpenFileRead will return if the file already exists - or not!
Code: Select all
IOresult = OpenFileRead(sFileName, nFileSize, fHandle);
if (IOresult == LDR_SUCCESS) {
for (i=0; i<RecLen; i++) {
ReadLn (fHandle, ibuf);
//...*SNIP*
Re: NXC Questions
Posted: 25 Feb 2011, 21:36
by timpattinson
Good tutorial on NXC, see pg 47 for an explanation of the filesystem
- NXC_tutorial.pdf
- Programming
LEGO NXT Robots
using NXC
(beta 30 or higher)
(Version 2.2, June 7, 2007)
by Daniele Benedettelli
with revisions by John Hansen - (312.53 KiB) Downloaded 492 times