Solved

how to ad one line to the end of a sequentiell file

Posted on 2003-11-24
11
396 Views
Last Modified: 2010-05-01
I have to add one line to an existing sequentiell line,
so if the file looks as follows

line1
line2
line3
end

then I have to write
text line 4
behind line3

How do I do it with the
PRINT statement

can anyone give some coding??
0
Comment
Question by:themroc
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 2

Accepted Solution

by:
molar earned 80 total points
ID: 9812096
You would use the WRITE statement instead of the print statement. To add the line to the end, you need to open the file using the Append keyword as below

dim fh as integer
fh = freefile

open app.path + "\test.txt" for append as fh
write #fh,"Text line 4"
close fh
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 20 total points
ID: 9812152
is the line reading 'end' actually in the file, as the last record, and what you want to have is:


line1
line2
line3
line4
end


????  AW

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9813899
If the "end" is not actually in the file, then molars code will work just fine.  If you are actually wanting to insert the line before the "end" line, then you have two choices:

1) Read the entire file into memory then write it back out adding your line before the end.

2) Read the file and write a second file as you go.  Then delete the original and rename the second file to the first.

Idle_Mind
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9815740
my point, EXACTLY.

AW
0
 
LVL 2

Assisted Solution

by:MaxPol
MaxPol earned 20 total points
ID: 9818209
Hi,

why don't you open it for append?

If you open the file for append, then you could easily use

print #1,"your last line" & vbcrlf



Hope this help,

Max
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:MaxPol
ID: 9818230
Hi,

forgot to say that to open it for append you should do like this:

Open "your file name" For Append Access Write As #1


Bye,

Max
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9818725
MaxPol:

because:

>If you open the file for append, then you could easily use

>print #1,"your last line" & vbcrlf


will end up with:


line1
line2
line3
end
line4

id 'end' is REALLY present as the last record in the original file.  If that is what is desired then OK, but otherwise the other comments apply.

AW
 
0
 
LVL 2

Expert Comment

by:MaxPol
ID: 9818996
Ops,

then you could try to find the file size (use the FileLen command),

now open the file in random mode read write , and read data at the countrary (read byte by byte with reclen=8

if after end you have a vbcrlf, then do like this
Dim sString as string * 8
Dim lFilePos as long

lFilePos=FileLen("yourfilenamehere"
get #1,lFilePos-8,sString ' this will read the vbcrlf & "end" & vbcrlf  if after end you have vbcrlf if not, change the reclen=6 and sString to string *6 and lFilePos-6
Now you are in the right place,
do a Print#1, "the new line" & vbcrlf & "end" ' if required add vbcrlf

Bye,

Max



0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9819106
sub apptext(filename as string, stext as string)
dim i as integer,buff as string
i=freefile

open filename  for input as #i
       buff=input(lof(i),1)
close#i

open filename  for output as #i
       print#i,replace(buff,"end",stext,,vbtextcompare)
       print #i,"end"
close#i
end sub
0
 

Author Comment

by:themroc
ID: 9827822
thanks for the contributions, i am sorry that I could not reply earlier to say that the first answer was alresdy what I was after, but the additional comments were very helpfull

:}
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9831501
well, if you finally use the first comment, take in mind the comments from Arthur_Wood.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Window placement 17 75
Write data from SQL to CSV file - position 1 52
How to make an ADE file by code? 11 79
Protecting vb6 & .Net code Obfuscation 18 94
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now