Omnimaga

Omnimaga => Our Projects => Ndless => Topic started by: battlestar on January 21, 2013, 07:36:26 pm

Title: Ndless v3.1 Read text file
Post by: battlestar on January 21, 2013, 07:36:26 pm
I am currently working on a port of battlestar(from bsd-games package) to the nspire. To get comfortable with programming, I am learning how to do the basics. I have mastered basic input and output, but I am clueless as how to read a file, say "file.tns". It is all text.

Right now my code is something like this: (not exactly, but all the code pertaining to the file reading)

Code: [Select]
FILE *test;

test = fopen("file.tns", "rt");
char line[80];
while(fgets(line, 80, test) != NULL) {
   displn(line,1); //Display string with newline
}
fclose(test);

I have "file.tns" in the same directory as my program on the nspire.

The nspire displays gibberish.
Any suggestions? I am using the latest version of ndless.
Title: Re: Ndless v3.1 Read text file
Post by: Levak on January 21, 2013, 09:26:07 pm
Hi
Sadly you have to indicate the full path (if someone has the solution im interested :p).

So "/documents/file.tns" is the root.
You can get the program path using argv[0]
Title: Re: Ndless v3.1 Read text file
Post by: battlestar on January 21, 2013, 09:34:10 pm
So I could use:
Code: [Select]
FILE *test;

test = fopen("/documents/devel/file.tns", "rt");
char line[80];
while(fgets(line, 80, test) != NULL) {
   displn(line,1); //Display string with newline
}
fclose(test);

And it would work?
Title: Re: Ndless v3.1 Read text file
Post by: battlestar on January 21, 2013, 09:59:25 pm
It still displays nothing.
Title: Re: Ndless v3.1 Read text file
Post by: Levak on January 21, 2013, 10:00:24 pm
Not sure what is "rt" but yeah, should work if the devel directory exits.
Title: Re: Ndless v3.1 Read text file
Post by: Rhombicuboctahedron on January 21, 2013, 10:00:51 pm
One thing I tried doing to learn how to use it was to look through all the files of other programs, since they had all the source released with them.
I remember torigolo released nrgblib with a tile game that looked through files on the calc, so that might be a good example
Then again I never was able to make any programs more than text based and combo box based stories with the text in one file, so my advice might not be good
And welcome to omnimaga!
Title: Re: Ndless v3.1 Read text file
Post by: epic7 on January 21, 2013, 10:02:59 pm
You might want to go visit nTxt (which is a text editor) and look at the source.
http://ourl.ca/17190
Title: Re: Ndless v3.1 Read text file
Post by: battlestar on January 21, 2013, 10:06:00 pm
I've looked at the sources, but they are confusing.  ???

However, I just figured it out. I used "rt", when I should have been using just "r".
Thank you for your help.
Title: Re: Ndless v3.1 Read text file
Post by: epic7 on January 21, 2013, 10:06:52 pm
I don't understand the source at all either :P
At least the solution was a pretty simple one :)
Title: Re: Ndless v3.1 Read text file
Post by: lkj on January 22, 2013, 02:57:05 pm
I've looked at the sources, but they are confusing.  ???
I don't understand the source at all either :P
:P
Lol, I wonder if my coding style is so bad that no one understands it :P
Title: Re: Ndless v3.1 Read text file
Post by: battlestar on January 22, 2013, 05:27:41 pm
Well, I have only recently learned c. I admire the structure of your code, I just didn't understand the file reading part.