INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

read file line by line from line 457Th

read file line by line from line 457Th

(OP)
I use the tipcal free file and do while eof ecc.... to read a lines line by line ina txt file.
is possible to start the reading from line 457 to the end of file, and not from the top of txt file?

Note:
my txt is approx 656 Mb and contain ca. 4.500.xxxx lnes

RE: read file line by line from line 457Th

Short anwser: no.

You are asking something close to "How can I travel from London to Cape Town by starting from Lisbon instead of London." The only answer is "first travel from London to Lisbon."

I.e. you must read/skip the records you don't care about.

The only (very rare) exception is to have previously recorded the byte address of the start of the record you want. Then you can Seek there before reading.


These forums are for professionals, not casual users and hobbyists.

RE: read file line by line from line 457Th

I just played around enough to skip three lines, zero based start postions, and then read from there:

CODE

Private Sub Command1_Click()

    Dim fso As New FileSystemObject
    Dim f As File
    Dim fsoStream As TextStream
    Dim strLine As String
    Set f = fso.GetFile("c:\junk.txt")
    Set fsoStream = f.OpenAsTextStream(ForReading)
    
    fsoStream.Skip (3)
    
    Do While Not fsoStream.AtEndOfStream
         strLine = fsoStream.ReadLine
         MsgBox strLine
    Loop
    
    fsoStream.Close
    Set fsoStream = Nothing
    Set f = Nothing
    Set fso = Nothing


End Sub 

It needs error handling and the like.

RE: read file line by line from line 457Th

Skip skips (by reading and discarding) the designated number of characters, not lines. So no, that isn't a solution.

The filesystemobject's textstream does have a skipline method (which reads a line and discards it) - you just need to call it 456 times; as dilettante says, unless you know the byte position of the start of line 457, then you have little option but to read the first 456 lines.

RE: read file line by line from line 457Th

I came back to mention that a reference to Microsoft Scripting Runtime needs to be added and saw strongm's response. Yes I can't argue that. To some degree it seems pedantic though. Functionally, what it accomplishes is to skip the lines. Couldn't you also say:

Dim x as string
dim y as string

X = "XX"
Y = "YY"

If X = Y then
'do something
End if

You really aren't comparing X to Y. The compiler generates machine language to compare areas in memory byte by byte. There are a myriad of functions that you could state, in English terms, it is doing that. And the come right back and say in machine language it isn't doing that at all.

Just my two cents...

RE: read file line by line from line 457Th

Nothing to do with the machine language. Or pedantry. Skipline is simply a non-value returning alias for ReadLine (just as Skip is an alias for Read).

fsoStream.Skip (3)

does exactly the same as

fsoStream.Read (3)

(and you can safely lose the brackets around the parameter, btw)

Please note that I'm not arguing that using Skipline is not a valid way of getting to line 457, simply pointing out that you can't start at line 457.

RE: read file line by line from line 457Th

Sigh...OK thumbsup2

RE: read file line by line from line 457Th

I really wish you could edit here...On second thought I see your point completely.

RE: read file line by line from line 457Th

Opening a file for random allows you to "skip" lines and start at the position you nominate.
Is there a perhaps way of doing this with a text file given that each "line" you want to skip ends with a CR or LF (or both)?

I would think you would have to somehow make the text file "look like" a random file (probably in advance).

RE: read file line by line from line 457Th

That's not how 'random' files work.

RE: read file line by line from line 457Th

If Line Inputted (beforehand) each "record" (the bit between CRLFs) and copied them to a new file, padding out each record to the same length with spaces, would this be equivalent to a random file that could be accessed by Get?

RE: read file line by line from line 457Th

Well, yes. If we turn the data source into a fixed width file, then sure, we can seek straight to the right line, as both dilettante and I have pointed out.

RE: read file line by line from line 457Th

Where exactly have you two pointed out the concept of " < we can seek straight to the right line, as both dilettante and I have pointed out. > "
I can't seem to find it in either of your posts?

dilettante only mentions using a byte address for the start of each line (of unequal size) and you explain "skipline" which I presume does not apply to a random file.

RE: read file line by line from line 457Th

If a 'record' has a fixed length then we can easily calculate the byte position of the 457th record and start reading data there. That is the point that dilettante and I have both made.

>recorded the byte address of the start of the record you want. Then you can Seek there before reading
>unless you know the byte position of the start of line 457, then you have little option but to read the first 456 lines.

And there is nothing special about a random access file that prevents us opening it for sequential access (or a mixed mode of random and sequential), and thus using sequential methods such as skipline.

RE: read file line by line from line 457Th

I beg to differ -

>recorded the byte address of the start of the record you want. Then you can Seek there before reading
This requires pre-knowledge of the file or opening each previous record to find it, nothing to do with "size" of individual records that I suggested.

>unless you know the byte position of the start of line 457, then you have little option but to read the first 456 lines.
I cant see this has anything to do with using the size of individual records that I suggested.

I was suggesting a possible method of how to initially do this only if you needed numerous queries on the same file, no advantage if you only wanted to do it once. Once done you would have the "position" of every line, not just the 456th line.

<(or a mixed mode of random and sequential)> I haven't come across this one. How do you do it (in vb6)?

It would be interesting to see if it would be faster converting the original file of this size into a database table or and simply moving to the required record?

RE: read file line by line from line 457Th

Er ... if I know the size of a fixed length record in my file, and know which record I want, then calculating that byte position is somewhat trivial.

RE: read file line by line from line 457Th

(OP)
Sorry me, but reality i need to jump and read the line 457.xxx form 4.500.xxx Lines.
note:
The txt ha e a fixed lenght 132 for each Lines

RE: read file line by line from line 457Th

Then open it AS RANDOM of length 132 (or maybe + or - 2?) and use Get. (See help on OPEN FILE)
Get the first few to see if you get each whole line each time and vary the 132 until you get an exact line each time.
Then go and Get the line you want.

What do you mean by the .xxx in your question?

RE: read file line by line from line 457Th

(OP)
.xxx

approx 4.xxx.xxx of lines

X is undefined number...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close