Solved

Write to a text file.

Posted on 1998-09-02
8
165 Views
Last Modified: 2010-04-30
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
Comment
Question by:checkout
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:waty
ID: 1432551
        nPrintOut = FreeFile
         Open sPSOut For Binary Access Write As #nPrintOut
         
         ' *** Add lines

         ' *** Close output file
         Close #nPrintOut

0
 
LVL 14

Expert Comment

by:waty
ID: 1432552
NB : I forgot to show you sample of writing :

       Put #nPrintOut, , "startrub" & NEW_LINE
       Put #nPrintOut, , "(" & sItem & ") putpgnum" & NEW_LINE
.
0
 

Author Comment

by:checkout
ID: 1432553
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 14

Accepted Solution

by:
waty earned 50 total points
ID: 1432554
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
 

Author Comment

by:checkout
ID: 1432555
I got your example, but it deleted the line that was in there and put this one inplace:

  startrub () putpgnum


Whats wrong?

Thanks

0
 

Author Comment

by:checkout
ID: 1432556
Thank you.
0
 
LVL 14

Expert Comment

by:waty
ID: 1432557
Try my second sample, it is better. The first one opened the file as binary. I do like this here at work.
0
 
LVL 1

Expert Comment

by:rayford
ID: 1432558
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

809 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