473,378 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Reading a specfic line in a text file.

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
Nov 21 '05 #1
6 1837
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Adrian Lin" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 21 '05 #2
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.

--
Ken Dopierala Jr.
For great ASP.Net web hosting try:
http://www.webhost4life.com/default.asp?refid=Spinlight

"Adrian Lin" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 21 '05 #3
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" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 21 '05 #4
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" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 21 '05 #5

"Adrian Lin" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 9/22/2004
Nov 21 '05 #6
Are there really so many that you cannot store them in memory until the end
of the run?

--
Jonathan Allen
"Adrian Lin" <Adrian Li*@discussions.microsoft.com> wrote in message
news:DA**********************************@microsof t.com...
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

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Suresh Kumaran | last post by:
Hi All, Does anybody know the sytax in VB.NET to write the contents of a multiline text box to a text file? Appreciate help. Suresh
7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
2
by: Roland Hall | last post by:
I have two(2) issues. I'm experiencing a little difficulty and having to resort to a work around. I already found one bug, although stated the bug was only in ODBC, which I'm not using. It...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
0
by: Adrian Lin | last post by:
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...
4
by: srikanthv | last post by:
I have below Sample text. I need find in the text specfic "Key name" its Value "HKEY_CURRENT_USER\Control Panel\International", If I find this I need to go inside of this and read Value 0 (Name:...
21
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
3
by: xyz | last post by:
Hi, I have a text file around 7GB includes 100 million lines... I want to read the data line by line when I approach my module.. ie., when i read for the first time , my program shuld read only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.