Author Topic: Kill v's Output  (Read 2471 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Kill v's Output
« on: December 09, 2020, 10:32:52 am »
I have an Update routine which presently uses "Open for Output" to completely overwrite the preexisting file of the same name. It has worked so far albeit the file being overwritten is getting pretty large over time. I feel if it aint broke etc but on the other hand if there is a better way then... Is there an advantage to using the command KILL instead of OPEN for OUTPUT ?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Kill v's Output
« Reply #1 on: December 09, 2020, 11:11:30 am »
Open for Output should rewrite the whole file each time you use it, no need to KILL.

What does keep growing is Open for Binary or Open for Append.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Kill v's Output
« Reply #2 on: December 09, 2020, 11:39:25 am »
Yeah, a file should only grow if you open it for APPEND or BINARY. Not for OUTPUT. Can you paste your code?
Shuwatch!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Kill v's Output
« Reply #3 on: December 09, 2020, 12:03:41 pm »
Code: QB64: [Select]
  1.     File$ = Drive$ + "P1PMC"
  2.     IF _FILEEXISTS(File$) THEN KILL File$
  3.     File$ = Drive$ + "P1PMC"
  4.     OPEN File$ FOR OUTPUT AS #1
  5.     WRITE #1, Event1 '        
  6.    
  7. FOR Ev = 1 TO 50
  8.         FOR mc = 1 TO 44
  9.             WRITE #1, P1PMC(Ev, mc)
  10.         NEXT mc
  11.     NEXT LN
  12.     CLOSE 1
  13.  
  14.     '' Fill Position 1 negative
  15.     File$ = Drive$ + "P1NMC"
  16.     IF _FILEEXISTS(File$) THEN KILL File$
  17.     File$ = Drive$ + "P1NMC"
  18.     OPEN File$ FOR OUTPUT AS #2
  19.  
  20.  

This is an example the code that I have been using. There are about 12 files storing the data, one set is storing Positive changes to 50 predefined events . Another is storing Negative changes to the same 50 events. The program loads in the data, calculates the changes then completely re-writes the files again with the updated changes. Over time I have discovered new events to track that become meaningful and it is only then that the new data will expand the old file.

Originally I used "KILL" because it appeared I'd be able to use wildcards but I haven't yet needed that feature. Now as I look at the code I'm thinking all I really need here is Open for Output.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Kill v's Output
« Reply #4 on: December 09, 2020, 12:10:23 pm »
KILL permanently deletes a file.

OPEN file FOR OUTPUT erases any old file of the same name and creates a blank one.

One gets rid of a file, the other just blanks it — which is two different things.

If you check some of my programs, you’ll see I use this trick a lot:

OPEN “temp.txt” FOR OUTPUT AS #1: CLOSE
OPEN “temp.txt” FOR BINARY AS #1

Since I tend to use the same filename over and over for temporary files, for testing purposes, I’ve developed the OUTPUT-BINARY style to make certain the current file I’m starting with is blank and doesn’t hold previous, unwanted data.  A similar process would be:

IF _FILEEXISTS(“temp.txt) THEN KILL “temp.txt”
OPEN “temp.txt” FOR BINARY AS #1

I just find the first preferable, as I can copy/paste that line and just change output to binary and be done with it, rather than having to type both lines, but both will basically work for the same purpose here.  ;)

Since you’re just blanking old information, either KILL or OUTPUT will work for your needs, so just choose whichever seems the most natural to you.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!