sheets1
asked on
Deleting lines in a file?
how can i open a file in VB and delete the first 3 lines?
is there a deletelines function or something like that?
is there a deletelines function or something like that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There's no direct process which can delete lines at top (or middle) of a sequential file. The only thing you can do is "to copy" the file into another, discarding the first lines as Jjbyers told you.
ASKER
Thanks to the both of you BUT i have another problem in that i want to delete the very last line in the file. how could i do this?
You could read the lines into an array and then only output to another file the lines you want.
To truncate the last line, you can do this:
Dim B as byte : open <your file> for binary as 1 : s&=lof(1)
count%=0
do 'Read the file backwards until find the 2nd <LF>
s&=s&-1 : seek #1,s& : Get #1,1,B
if b=10 then count%=count%+1 : if count%=2 then exit do
loop
a$="" : put #1,,a$ : close #1
After completion of that, the file is truncated at the point where a <LF> byte is found, getting rid of the last line.
Watch out: this assumes file has AT LEAST two lines.
Dim B as byte : open <your file> for binary as 1 : s&=lof(1)
count%=0
do 'Read the file backwards until find the 2nd <LF>
s&=s&-1 : seek #1,s& : Get #1,1,B
if b=10 then count%=count%+1 : if count%=2 then exit do
loop
a$="" : put #1,,a$ : close #1
After completion of that, the file is truncated at the point where a <LF> byte is found, getting rid of the last line.
Watch out: this assumes file has AT LEAST two lines.