Solved

Input/Output with files?

Posted on 1999-01-14
11
152 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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
 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month7 days, 1 hour left to enroll

622 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