Reading from multiple lines in a File

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
logebotmaster
Posts: 6
Joined: 21 Oct 2010, 04:00

Reading from multiple lines in a File

Post by logebotmaster »

Hi,
Okay, I know how to write multiple lines into a file on the NXT, and it's proven very helpful in creating readable .txt datalogs. What my question is, is it possible for the NXT to retrieve information from a file in any of the written lines instead of just the last recorded line? And also, is it possible to accurately retrieve data from a .txt file? Thanks
Botmaster
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Reading from multiple lines in a File

Post by mattallen37 »

Well, with NXC, Yes, and Yes.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Reading from multiple lines in a File

Post by muntoo »

logebotmaster wrote:Hi,
What my question is, is it possible for the NXT to retrieve information from a file in any of the written lines instead of just the last recorded line?
Matt, I think that the OP was also asking "how".

Assuming it's in string format, you may want to check out ReadLnString().
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Reading from multiple lines in a File

Post by muntoo »

I can't edit my post...

Anyways, here it is again:

@Matt, I think that the OP was also asking "how".
logebotmaster wrote:Hi,
What my question is, is it possible for the NXT to retrieve information from a file in any of the written lines instead of just the last recorded line?
Assuming it's in string format, you may want to check out ReadLnString().

I believe it works like this:

Code: Select all

void ReadLines(string &out[], string filename)
{
    byte handle;
    unsigned int fsize;

    ArrayInit(out, "", 0);

    if(OpenFileRead(filename, fsize, handle) == LDR_SUCCESS)
    {
        // Yes, I know I'm becoming more and more like the guys at IOCCC
        for(string szBuf; (ReadLnString(handle, szBuf) == LDR_SUCCESS); ArrayBuild(out, out, szBuf));

        CloseFile(handle);
    }
}
Last edited by muntoo on 21 Dec 2010, 23:18, edited 1 time in total.
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Reading from multiple lines in a File

Post by mattallen37 »

I suppose you are likely right. Here is an example of the method I use.

Code: Select all

if(OpenFileRead("File.txt", size, handle) == NO_ERR)
{
  until (Sdone == true)  // read the text file till the end
  {
    if(ReadLnString(handle,message) != NO_ERR) Sdone = true;
    //You need to grab the message and write it to another string(or whatever you need to do) all right here.
    //Base it on an increasing variable, like the following lines.
    TotalMessage[i]=message;
    i++
  }
}
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
dudmaster
Posts: 171
Joined: 06 Oct 2010, 02:38
Location: Texas, Santa Fe
Contact:

Re: Reading from multiple lines in a File

Post by dudmaster »

[quotename=mattallen]

Code: Select all

if(OpenFileRead("File.txt", size, handle) == NO_ERR)
{
  until (Sdone == true)  // read the text file till the end
  {
    if(ReadLnString(handle,message) != NO_ERR) Sdone = true;
    //You need to grab the message and write it to another string(or whatever you need to do) all right here.
    //Base it on an increasing variable, like the following lines.
    TotalMessage[i]=message;
    i++
  }
}
[/quote]
I need to write this to the variable "Sen". Can you modify this
program to do this?

And how does muntoo do the "Show" thing???? :evil:
2Labz.com, My Website
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Reading from multiple lines in a File

Post by muntoo »

dudmaster wrote: I need to write this to the variable "Sen". Can you modify this
program to do this?

And how does muntoo do the "Show" thing???? :evil:
One of these buttons contains the answer. Actually, they all do.
Image
Image

Code: Select all

[spoiler][url=http://xkcd.com/109/][spoiler][img]http://imgs.xkcd.com/comics/spoiler_alert.png[/img][/spoiler][/url]
I use spoilers

Anyways, enough fooling around before Xander gets tempted to remove the "Show button" ;). Here's what you want, assuming that Sen is an array of strings:

Code: Select all

void ReadLines(string &out[], string filename)
{
    byte handle;
    unsigned int fsize;

    ArrayInit(out, "", 0);

    if(OpenFileRead(filename, fsize, handle) == LDR_SUCCESS)
    {
        // Yes, I know I'm becoming more and more like the guys at IOCCC
        for(string szBuf; (ReadLnString(handle, szBuf) == LDR_SUCCESS); ArrayBuild(out, out, szBuf));

        CloseFile(handle);
    }
}


task main()
{
    string Sen[];
    ReadLines(Sen, "File.txt");
}
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
dudmaster
Posts: 171
Joined: 06 Oct 2010, 02:38
Location: Texas, Santa Fe
Contact:

Re: Reading from multiple lines in a File

Post by dudmaster »

Sen is a string (not array). I like your idea. Thanks... half
2Labz.com, My Website
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Reading from multiple lines in a File

Post by muntoo »

dudmaster wrote:Sen is a string (not array). I like your idea. Thanks... half
If Sen is JUST a string, I don't get why you don't just do this:

Code: Select all

OpenFileRead(fname, fsize, handle);
Read(handle, Sen);
//Or if that doesn't work:
ReadBytes(handle, fsize, Sen);
//Or
char cSen[];
ReadBytes(handle, fsize, cSen);
CloseFile(handle);
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
logebotmaster
Posts: 6
Joined: 21 Oct 2010, 04:00

Re: Reading from multiple lines in a File

Post by logebotmaster »

Thanks all, very interesting indeed.
Botmaster
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests