Solved

Deleting lines in a file?

Posted on 1999-01-20
5
167 Views
Last Modified: 2010-05-03
how can i open a file in VB and delete the first 3 lines?
is there a deletelines function or something like that?
0
Comment
Question by:sheets1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
jjbyers earned 20 total points
ID: 1469660
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
 
LVL 3

Expert Comment

by:vikiing
ID: 1469661
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
 

Author Comment

by:sheets1
ID: 1469662
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
 
LVL 3

Expert Comment

by:jjbyers
ID: 1469663
You could read the lines into an array and then only output to another file the lines you want.
0
 
LVL 3

Expert Comment

by:vikiing
ID: 1469664
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question