?
Solved

Write to a text file.

Posted on 1998-09-02
8
Medium Priority
?
170 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Accepted Solution

by:
waty earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

801 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