Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Input/Output with files?

Posted on 1999-01-14
11
Medium Priority
?
153 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 40 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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 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…

721 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