Solved

Input/Output with files?

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
Put text in a picture ASP.NET C# 2 73
Added a column screws up code 5 78
Problem to With line 4 87
VB6 - Cannot type when doing an extract from MSHFlexgrid to Excel 6 47
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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…

751 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