• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Write to a text file.

What is the simplest way to write to a text file.  This is my routine:
Open "C:\windows\system\settings.txt" For Output As #1
Write #1, Something
 Close #1

I want to be able to write to the next available line with out deleting  the previous lines.  Also, I would like to know how to add a string to the right of any line I choose.

Thank you .
0
checkout
Asked:
checkout
  • 4
  • 3
1 Solution
 
watyCommented:
        nPrintOut = FreeFile
         Open sPSOut For Binary Access Write As #nPrintOut
         
         ' *** Add lines

         ' *** Close output file
         Close #nPrintOut

0
 
watyCommented:
NB : I forgot to show you sample of writing :

       Put #nPrintOut, , "startrub" & NEW_LINE
       Put #nPrintOut, , "(" & sItem & ") putpgnum" & NEW_LINE
.
0
 
checkoutAuthor Commented:
Hi Waty,

I don't doubt that your answer may work, but I am not sure how to use it.  Remember this file already exists it may have data in it.  Where you say add lines, my question is how?  I'm not really looking for theory, I need some example.Can you show me a working example, and I will give you the points.


Thank you
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
watyCommented:
As it is Text file, use the following code. It works well, I have just tested it.

   nPrintOut = FreeFile
   Open "d:\c.txt" For Append Access Write As #nPrintOut
   
   Print #nPrintOut, "Koko 1"
   Print #nPrintOut, "Koko 2"
       
   ' *** Close output file
   Close #nPrintOut

0
 
checkoutAuthor Commented:
I got your example, but it deleted the line that was in there and put this one inplace:

  startrub () putpgnum


Whats wrong?

Thanks

0
 
checkoutAuthor Commented:
Thank you.
0
 
watyCommented:
Try my second sample, it is better. The first one opened the file as binary. I do like this here at work.
0
 
rayfordCommented:
How this old Basic method of IO works is you can use either Print your output file line by line which is a variable length record using the EOL CR-LF as a record separator OR you can use Binary Random access which if I'm not mistaken requires a fixed record length.  So for you to be able to INSERT "records" you would need to waste some space by padding your output lines and switch over from the old PRINT and LINE INPUT technique to the BINARY RANDOM access RECORD method.  Decide how big the largest text line will be and simply space fill all records to this larger size abd you should have no trouble working with inserting and editing records then.  I've got some sample code and help from the VB books Online etc if you need it.  You may also want to simply look into using a database which will allow for better inserting and sorting of your records but this RANDOM access is the best for editing (adding to the right as you say) of existing records and appending both.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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