Solved

Deleting lines in a file?

Posted on 1999-01-20
5
166 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

749 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