NXC Questions
Re: NXC Questions
You should not open a file and the change the value stored in the handle variable that was returned by the open function. You need to close that file using CloseFile or fclose with the same handle value returned by OpenFileRead. A file handle should never be modified other than via calls to file i/o functions which set or read its value.
John Hansen
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC Questions
Sorry, but, find me a way to reach my target then?
If this helps: The file in the previous post will not be used anywhere after that part in the program (in this case), but it will be used if there is a way to increase its value each time by one.
If this helps: The file in the previous post will not be used anywhere after that part in the program (in this case), but it will be used if there is a way to increase its value each time by one.
B2O productions are the best for the best.
Re: NXC Questions
The handle is fixed by the software to refer to only one file. If you want to start a new file you can use the old file name but add 1 to that. The software will then give you a new handle for that file when you open it.
A sophistical rhetorician, inebriated with the exuberance of his own verbosity, and gifted with an egotistical imagination that can at all times command an interminable and inconsistent series of arguments to malign an opponent and to glorify himself.
Re: NXC Questions
I could fix your code if a) I had a copy of the entire program and b) you described clearly and carefully exactly the behavior you want your program to have. What you have posted so far is undecipherable - by me, at least.
John Hansen
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC Questions
Here is the program:
This is the whole program for now. What is it for? To a) check for a file existence, and if not, create the file (Done) and b) to add "1" to the file's value if the file is found.
The idea is that each time a person starts the program, one point is added to something. I will use this to show the set-up at the (PART X) if its the first time (something = 1) the user runs the program.
Thanks.
Code: Select all
byte handle;
int fsize;
task main () {
if(OpenFileRead("Visits.txt", fsize, handle) == LDR_SUCCESS)
{
// increase something by one, maybe the file value itself
}
else if(OpenFileRead("Visits.txt", fsize, handle) == LDR_FILENOTFOUND)
{
CreateFile("Visits.txt", 512, handle);
// increase something by one, maybe the file value itself
}
if(_______ == 1) // if "something" = 1
{
TextOut(8,32,"One");//this part is just to make sure that it works, but actually
Wait(5000);// i will do something else in it. (PART X)
}
}
The idea is that each time a person starts the program, one point is added to something. I will use this to show the set-up at the (PART X) if its the first time (something = 1) the user runs the program.
Thanks.
B2O productions are the best for the best.
Re: NXC Questions
Here is a program that may suit your needs. Please read the comments and if you have any questions do not hesitate to ask.
John Hansen
Code: Select all
const string visit_filename = "Visits.txt";
const string tmp_filename = "___tmp.txt";
long ReadVisitInformation() {
unsigned int fsize;
byte handle;
long result = 0;
if (OpenFileRead(visit_filename, fsize, handle) == LDR_SUCCESS) {
// read existing value
Read(handle, result);
// close the file
fclose(handle);
}
return result;
}
void SaveVisitInformation(long visits) {
byte handle;
// create temporary file
if (CreateFile(tmp_filename, 256, handle) == LDR_SUCCESS) {
// write the new visit value to the file
Write(handle, visits);
// close the temporary file
fclose(handle);
// delete old visit file
remove(visit_filename);
// rename temp file
rename(tmp_filename, visit_filename);
}
}
task main () {
long oldVisits = ReadVisitInformation();
if(oldVisits > 0)
{
TextOut(0,LCD_LINE1,"Welcome back");
TextOut(0,LCD_LINE2,"visit number");
NumOut(0,LCD_LINE3,oldVisits);
Wait(SEC_5);
// do NOT show user first time information
}
else
{
TextOut(0,LCD_LINE1,"I see that");
TextOut(0,LCD_LINE2,"this is your");
TextOut(0,LCD_LINE3,"first time");
Wait(SEC_5);
// ShowUserFirstRunInfo();
}
// now update the visits number
oldVisits++;
SaveVisitInformation(oldVisits);
}
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC Questions
First of all, thank you.
I got a general question:
-I can see that NXC isn't as simple as i thought, after i saw your code. How can i think in such a code (yours)? If this is for a simple idea, what will i do when i want to make a more advanced one? What to do to learn?
Ok, now the code questions:
1) What are those commands: SaveVisitInformation, ReadVisitInformation, ...etc.
2) Why did you make the temp. file?
In the code:
- If its the second time, it shows 1. How to fix this? (I tried "oldVisits += 1;" in the "if(oldVisits > 0);", it works the first time but after that it shows the multiples of 2. i.e.:
Visit No. ---------- Shows
1 ------------------ I see that it's the first time
2 ------------------ 2
3 ------------------ 4
4 ------------------ 6
...etc
------------
Have a nice day.
I got a general question:
-I can see that NXC isn't as simple as i thought, after i saw your code. How can i think in such a code (yours)? If this is for a simple idea, what will i do when i want to make a more advanced one? What to do to learn?
Ok, now the code questions:
1) What are those commands: SaveVisitInformation, ReadVisitInformation, ...etc.
2) Why did you make the temp. file?
In the code:
- If its the second time, it shows 1. How to fix this? (I tried "oldVisits += 1;" in the "if(oldVisits > 0);", it works the first time but after that it shows the multiples of 2. i.e.:
Visit No. ---------- Shows
1 ------------------ I see that it's the first time
2 ------------------ 2
3 ------------------ 4
4 ------------------ 6
...etc
------------
Have a nice day.
B2O productions are the best for the best.
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC Questions
When John used to give me example programs in NXC, I felt the same way. I realized that I needed to learn more about NXC (as you have realized as well). What I did, was take the programs apart, and make "test" programs for almost every function. I would stick to it until I understood what it did, and how to use it to benefit me. I still build/program things just to test, and confirm what I think should happen. For instance, my Rock Crawler. I built that to test some mechanical ideas, but also to test some programming things (like my RS485 library). Everything worked almost exactly as I expected/hoped, and so the project served it's purpose. You should build/program to test ideas as well. Experience is usually the best teacher.borntoown wrote:-I can see that NXC isn't as simple as i thought, after i saw your code. How can i think in such a code (yours)? If this is for a simple idea, what will i do when i want to make a more advanced one? What to do to learn?
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC Questions
The value in oldVisits is the number of visits prior to this one. If you want to output a value that is the number of visits prior to this one plus this one then you could output oldVisits+1 instead of oldVisits.
The two functions simply encapsulate (kind of like a MyBlock in NXT-G) a piece of functionality that you either want to reuse or separate it from the rest of the code in a task or a function so that it doesn't clutter or confuse the rest of the code. Writing to a file or reading from a file that is just for keeping track of the number of times a person has executed your program probably should not be mixed in with the main logic of your program. By putting that code into separate functions it makes it a lot easier to see what task main is doing.
The reason for a temporary file is that with the standard NXT firmware you cannot write to an existing file except at its end. So you need to create a new file if "Visits.txt" already exist and delete the old one. You could delete the old one and then create a new one called "Visits.txt" but if, for some reason, the attempt to create the file fails you are in a bind since you already deleted the old one. It is safer to write the new visit value to a file with a temporary name and if that succeeds then delete the old file and rename the newly created one.
You really need to read tutorials and language guides and look at all the sample programs that come with BricxCC and are on the NBC website.
John Hansen
The two functions simply encapsulate (kind of like a MyBlock in NXT-G) a piece of functionality that you either want to reuse or separate it from the rest of the code in a task or a function so that it doesn't clutter or confuse the rest of the code. Writing to a file or reading from a file that is just for keeping track of the number of times a person has executed your program probably should not be mixed in with the main logic of your program. By putting that code into separate functions it makes it a lot easier to see what task main is doing.
The reason for a temporary file is that with the standard NXT firmware you cannot write to an existing file except at its end. So you need to create a new file if "Visits.txt" already exist and delete the old one. You could delete the old one and then create a new one called "Visits.txt" but if, for some reason, the attempt to create the file fails you are in a bind since you already deleted the old one. It is safer to write the new visit value to a file with a temporary name and if that succeeds then delete the old file and rename the newly created one.
You really need to read tutorials and language guides and look at all the sample programs that come with BricxCC and are on the NBC website.
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Re: NXC Questions
I finally made my first step in NXC! I finished the first part of the project's program, which is to show the set up (Asking to set a password the first time and Asking to input the password the next times). I learned some things like the Conditional loop (while and break commands), the way to write on a file, and the most important thing is that i found NXC isn't that much hard as i thought. Little thinking will solve it.
Thanks, for the piece of coding that you gave me, afanofosc. I really appreciate it. Thank you, the rest of people, for helping in other stuff.
I will start working on the second part of the program. If i needed anything, i will ask you. Though, i like to try at the first to see if i can really continue in this.
Have a nice day,
B2O.
Thanks, for the piece of coding that you gave me, afanofosc. I really appreciate it. Thank you, the rest of people, for helping in other stuff.
I will start working on the second part of the program. If i needed anything, i will ask you. Though, i like to try at the first to see if i can really continue in this.
Have a nice day,
B2O.
B2O productions are the best for the best.
Who is online
Users browsing this forum: Semrush [Bot] and 1 guest