Reading string from file at specific line
Reading string from file at specific line
Hello,
I'm trying to read a stringvalue from a file.
The string is always located at line 15 of the file, but lines are not always of the same length, so I can't use seek.
The file isn't generated by me, so i can't change it in any way.
I use readstring at the moment, but it makes me read 15 lines, before i got the right line...
Is there any way to make a Lineseek, or something?
Thnx in advance for all suggestions.
I'm trying to read a stringvalue from a file.
The string is always located at line 15 of the file, but lines are not always of the same length, so I can't use seek.
The file isn't generated by me, so i can't change it in any way.
I use readstring at the moment, but it makes me read 15 lines, before i got the right line...
Is there any way to make a Lineseek, or something?
Thnx in advance for all suggestions.
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
You'll know it, when it gets released...
Code: Select all
Procedure NotL(Val)
ProcedureReturn (Not Val)
EndProcedure
ReadFile(0, "c:\HelloWorldApp.java")
I = 0
While I < 14 And NotL(Eof(0))
If ReadByte(0) = 10
I + 1
EndIf
Wend
Debug ReadString(0)
-
Bonne_den_kule
- Addict

- Posts: 841
- Joined: Mon Jun 07, 2004 7:10 pm
Why make it so hard Trond?
Code: Select all
ReadFile(0, "c:\HelloWorldApp.java")
I = 0
While I < 14 And Eof(0)=0
If ReadByte(0) = 10
I + 1
EndIf
Wend
Debug ReadString(0) you can also just do a loop of readstring() without affecting the value, it should be quite fast:
Code: Select all
ReadFile(0, "c:\HelloWorldApp.java")
For k=1 to 14
ReadString(0)
Next
Debug ReadString(0)Hmm, searching for a linefeed or using readstring, don't know what's faster, Trond. :roll:
That's almost exactly what I had Fred
But it does give me a brainwave getting the stringcheck not to be checked at first 14 lines...
That's almost exactly what I had Fred
But it does give me a brainwave getting the stringcheck not to be checked at first 14 lines...
Currently coding the unbelievable WFS program...
You'll know it, when it gets released...
You'll know it, when it gets released...
Doing 15 ReadStrings seems the simplest way.
Anyhow, just for fun, here is another way.
Anyhow, just for fun, here is another way.
Code: Select all
src.s = "Path\to\a\file\with\ascii.txt"
line.s = "??? Did not read file"
lineToRead = 15
lineToRead - 1 ; Index of preceding EOL
sz = FileSize(src)
If sz > 0
ReadFile(1, src)
If LineToRead = 0
line = ReadString(1)
Else
buf.s = Space(sz+1)
ReadData(1, @buf, sz)
p=1
For i = 1 To lineToRead
p = FindString(buf, #LF$, p) + 1 ; This Assumes either Chr(13)+Chr(10) or Chr(10) eol
If p = 1 ; If eol in your file is just Chr(13), use #CR$ instead
Break
EndIf
Next
If p <> 1
px = FindString(buf, #LF$, p)
line = Mid(buf, p, px-p-1) ; If just Chr(10) [or JUST Chr(13)] eol, drop the "-1"
Else
line = "*** not enough lines ***"
EndIf
EndIf
CloseFile(1)
EndIf
Debug line@}--`--,-- A rose by any other name ..
Very fast on small files:
Code: Select all
Procedure.s Test4()
ReadFile(0, "c:\HelloWorldApp.java")
Ptr.l
Ptr = AllocateMemory(Lof(0))
ReadData(0, Ptr, Lof(0))
A.s = StringField(PeekS(Ptr), 15, #CRLF$)
FreeMemory(Ptr)
CloseFile(0)
ProcedureReturn A
EndProcedureWaussie,
Your problem is that you don't know how long the lines are. To find that out, you have to read through the whole thing finding the line breaks.
For example, if the first 15 lines are empty you would only have to seek to the 16th Byte.
So theres is no way around it. If the linebreaks can be anywhere, your going to have to read through every byte until you reach the 15th line.
Your problem is that you don't know how long the lines are. To find that out, you have to read through the whole thing finding the line breaks.
For example, if the first 15 lines are empty you would only have to seek to the 16th Byte.
So theres is no way around it. If the linebreaks can be anywhere, your going to have to read through every byte until you reach the 15th line.



