Solved

Input/Output with files?

Posted on 1999-01-14
11
150 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
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.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 start screen issues 9 55
vb6 - Transfer from MSHFlexgrid1 to xls issue 8 56
Excel Automation VBA 19 91
Export Data to Different .csv Files 26 115
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

828 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