Reading a specfic line in a text file.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way to read a specfic line in a text file? For instance if I
wanted to just read the 5th line is that possible without having to step
through the other lines? Essentially I am doing huge calculations in which
genetic matrices are saved in text files instead of being held in arrays that
take up memory. I am trying to do something where the genetic matrix is
stored in a text file and I call specfic lines for calculations then put the
results in another text file. Any help would be great,

Thanks, Adrian
 
Hi Adrian,

I don't think there is a .Net way of doing that with file streams. What you
could do though to make it easier for you is create a helper function that
wraps the ReadLine function. You would pass this function the line you
want, your function would loop through and return the line you are looking
for. You are still looping but at least you would only have to write the
function once and you could call it from anywhere with 1 line of code.

An alternative is to create your text file so that all lines are the *exact*
same length. If you do this then you could just run a calculation as to
where in the file your data is. Then on the stream reader you can use the
BaseStream.Seek() function to go directly there. You'll probably need to
put in some filler characters, but filtering those out after you read the
line might be faster than looping. Good luck! Ken.
 
Adrian,
Do you know where the line starts in the file?

You could use Stream.Seek to position to that line, then you can read the
line.

Are the lines fixed length or variable length?

For fixed length lines, a simply calculation can find the offset to use.

For variable length lines, I would consider creating a second "index" file
that contains the offsets of each line as an Integer. A simply calculation
can find the offset to the index to use.

Post if you want examples.

Hope this helps
Jay
 
Adrian Lin said:
Is there a way to read a specfic line in a text file? For instance if I
wanted to just read the 5th line is that possible without having to step
through the other lines? Essentially I am doing huge calculations in which
genetic matrices are saved in text files instead of being held in arrays that
take up memory. I am trying to do something where the genetic matrix is
stored in a text file and I call specfic lines for calculations then put the
results in another text file. Any help would be great,

Thanks, Adrian

I don't know if it would be any faster, but..
IF the files are small - you could do one read per file -
and read the entire contents into a String,
then use some string manipulation to extract the data.
reading loops are expensive - timewise - this may speed things up
Hal
 
Are there really so many that you cannot store them in memory until the end
of the run?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top