Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file line by line from line 457Th

Status
Not open for further replies.

2009luca

Programmer
Jul 27, 2013
222
0
16
IT
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
 
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 [tt]Seek[/tt] there before reading.


These forums are for professionals, not casual users and hobbyists.
 
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.
 
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.
 
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...
 
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).

[tt]fsoStream.Skip (3)[/tt]

does exactly the same as

[tt]fsoStream.Read (3)[/tt]

(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.
 
I really wish you could edit here...On second thought I see your point completely.
 
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).
 
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?
 
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.
 
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.
 
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.
 
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?
 
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.
 
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
 
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?
 
.xxx

approx 4.xxx.xxx of lines

X is undefined number...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top