Author Topic: Ndless v3.1 Read text file  (Read 2856 times)

0 Members and 1 Guest are viewing this topic.

Offline battlestar

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +0/-0
    • View Profile
Ndless v3.1 Read text file
« 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.
« Last Edit: January 21, 2013, 09:34:15 pm by battlestar »

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Ndless v3.1 Read text file
« Reply #1 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]
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline battlestar

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +0/-0
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #2 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?

Offline battlestar

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +0/-0
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #3 on: January 21, 2013, 09:59:25 pm »
It still displays nothing.

Offline Levak

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1002
  • Rating: +208/-39
    • View Profile
    • My website
Re: Ndless v3.1 Read text file
« Reply #4 on: January 21, 2013, 10:00:24 pm »
Not sure what is "rt" but yeah, should work if the devel directory exits.
I do not get mad at people, I just want them to learn the way I learnt.
My website - TI-Planet - iNspired-Lua

Offline Rhombicuboctahedron

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 437
  • Rating: +41/-6
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #5 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!

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #6 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
« Last Edit: January 21, 2013, 10:03:58 pm by epic7 »

Offline battlestar

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +0/-0
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #7 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.

Offline epic7

  • Chopin!
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2200
  • Rating: +135/-8
  • I like robots
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #8 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 :)

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #9 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

Offline battlestar

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 14
  • Rating: +0/-0
    • View Profile
Re: Ndless v3.1 Read text file
« Reply #10 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.