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

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??
themrocAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

molarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Arthur_WoodCommented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Arthur_WoodCommented:
my point, EXACTLY.

AW
0
MaxPolCommented:
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
MaxPolCommented:
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
Arthur_WoodCommented:
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
MaxPolCommented:
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
Richie_SimonettiIT OperationsCommented:
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
themrocAuthor Commented:
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
Richie_SimonettiIT OperationsCommented:
well, if you finally use the first comment, take in mind the comments from Arthur_Wood.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

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.