Solved

Input/Output with files?

Posted on 1999-01-14
11
147 Views
Last Modified: 2010-05-03
I am designing a utility which reads in a file and outputs it to another file. However, when each line is being outputted it seems to write over the contents that already exist in the file - and i wish to add to the file not write over whats already there.

Is this possible?
0
Comment
Question by:sheets1
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 10

Accepted Solution

by:
caraf_g earned 10 total points
ID: 1456173
Yes, open the file for Append

See also the Help on the Open keyword.

Good luck!
0
 

Author Comment

by:sheets1
ID: 1456174
this is my code:

Open filename For Input As fh ' Open file.

path=c:\messagestore.dat

Do While Not EOF(fh) ' Loop until end of file.
    Line Input #fh, textline ' Read line into variable.
    Close fh
    Open Path For Append As #1
    Print #1, textline
Loop

firstly it creates a new file called "messagestore" rather than using the existing and also it only reads in one line of first file.
0
 
LVL 18

Expert Comment

by:deighton
ID: 1456175
Open filename For Input As fh ' Open file.
      path=c:\messagestore.dat
      open path for output as #2      'open an output file

      Do While Not EOF(fh) ' Loop until end of file.
          Line Input #fh, textline ' Read line into variable.
          Print #2, textline   'write to new file
      Loop

     close fh,#2    'close the file after the loop
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456176
this is your fixed code:

fh = FreeFile
Open filename For Input As fh ' Open file.

Dim intFile1 As Integer
intFile1 = FreeFile
path=c:\messagestore.dat

Open path For Append As #intFile1

Do While Not EOF(fh) ' Loop until end of file.
    Line Input #fh, textline ' Read line into variable.
    Print #intFile1, textline
Loop

Close fh
Close #intFile1

0
 
LVL 3

Expert Comment

by:vikiing
ID: 1456177
Consider that OPEN FOR OUTPUT statement **DESTROYS** file's contents; that's why you see "to write over the contents that already exist in the file".

On the other hand, OPEN FOR APPEND permits you to ADD data at the very end of the file, without disturbing what file already has.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:sheets1
ID: 1456178
thanks alot caraf_g - it worked!!

but is there away putting in a character return in the file without manually doing it - because it puts the first line after the current text - and i want it on a new line?

cheers


0
 

Author Comment

by:sheets1
ID: 1456179
thanks alot caraf_g - it worked!!

but is there a way of putting in a character return in the file without manually doing it - because it puts the first line after the current text - and i want it on a new line?

cheers


0
 
LVL 3

Expert Comment

by:vikiing
ID: 1456180
If you "print" text lines as Caraf_G told you (Print #intFile1, textline), each line has an implicit line-terminator, formed with two bytes known as <CR><LF> (ASCII 13 and 10 respectively).

With that, last line of file will have that terminator too, making the file to end with <CR><LF>. The next append you do will begin AFTER that last <CR><LF>, making unnecessary to add anything else because new text effectively will start in a new line.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456181
If the file you wish to append to doesn't already end with a new line the result will indeed be that the first line of the new text will be appended to the last line of the existing text.

This can be fixed quite easily by putting
Print #intFile1, ""
after the line that reads
Open path For Append As #intFile1

Of course if it does already end with a new line this will add a blank line to your append file, like vikiing says.

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 1456182
If you want to avoid this, the following will let you check the last character of your append file. If the ascii code for that is 10, the append file ends with a new line so you don't have to print a blank line, otherwise it doesn't so you do have to print a blank line:

Dim intF As Integer
Dim strT As String * 1
intF = FreeFile
Open "<your append file>" For Random As #intF Len = 1
Get #intF, FileLen("<your append file>"), strT
Close #intF
If Asc(strT) <> 10 Then
    'does not end with a new line, so
    'print a blank line.
Else
    'Doesn't end with a new line so you don't need to
    'print a blank line
End If

Do this before all the code you've already got.
0
 

Author Comment

by:sheets1
ID: 1456183
thanks alot caraf_g!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

15 Experts available now in Live!

Get 1:1 Help Now