Reading from multiple lines in a File
- 
				logebotmaster
 - Posts: 6
 - Joined: 21 Oct 2010, 04:00
 
Reading from multiple lines in a File
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
			
			
									
						
										
						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
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
			
						http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Reading from multiple lines in a File
Assuming it's in string format, you may want to check out ReadLnString().
Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Reading from multiple lines in a File
I can't edit my post...
Anyways, here it is again:
@Matt, I think that the OP was also asking "how".
Assuming it's in string format, you may want to check out ReadLnString().
I believe it works like this:
			
			
													Anyways, here it is again:
@Matt, I think that the OP was also asking "how".
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.
									
			
						
							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
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
			
						http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: Reading from multiple lines in a File
[quotename=mattallen][/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????
			
			
									
						
							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++
  }
}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????
2Labz.com, My Website
			
						Re: Reading from multiple lines in a File
Anyways, enough fooling around before Xander gets tempted to remove the "Show button"
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");
}Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE
Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Re: Reading from multiple lines in a File
Sen is a string (not array). I like your idea. Thanks... half
			
			
									
						
							2Labz.com, My Website
			
						Re: Reading from multiple lines in a File
If Sen is JUST a string, I don't get why you don't just do this:dudmaster wrote:Sen is a string (not array). I like your idea. Thanks... half
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);
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
Thanks all, very interesting indeed.
Botmaster
			
			
									
						
										
						Botmaster
Who is online
Users browsing this forum: No registered users and 5 guests
