Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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?
0
sheets1
Asked:
sheets1
  • 2
  • 2
1 Solution
 
jjbyersCommented:
I'm assumming a Text file.

open "Filename" for input as #1
open "Newfilename" for output as #2
cnt=0
do until eof(1)
  line input #1,a$
  if cnt>2 then
    print #2,a$
  end if
  cnt=cnt+1
loop
close #1
close #2
0
 
vikiingCommented:
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.

0
 
sheets1Author Commented:
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?
0
 
jjbyersCommented:
You could read the lines into an array and then only output to another file the lines you want.
0
 
vikiingCommented:
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.


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now