Page 1 of 1

Reading from multiple lines in a File

Posted: 21 Dec 2010, 20:02
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

Re: Reading from multiple lines in a File

Posted: 21 Dec 2010, 20:51
by mattallen37
Well, with NXC, Yes, and Yes.

Re: Reading from multiple lines in a File

Posted: 21 Dec 2010, 21:33
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().

Re: Reading from multiple lines in a File

Posted: 21 Dec 2010, 21:43
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);
    }
}

Re: Reading from multiple lines in a File

Posted: 21 Dec 2010, 21:53
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++
  }
}

Re: Reading from multiple lines in a File

Posted: 21 Dec 2010, 23:56
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:

Re: Reading from multiple lines in a File

Posted: 22 Dec 2010, 00:42
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");
}

Re: Reading from multiple lines in a File

Posted: 22 Dec 2010, 02:27
by dudmaster
Sen is a string (not array). I like your idea. Thanks... half

Re: Reading from multiple lines in a File

Posted: 23 Dec 2010, 04:23
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);

Re: Reading from multiple lines in a File

Posted: 28 Dec 2010, 22:41
by logebotmaster
Thanks all, very interesting indeed.
Botmaster