Author Topic: File managing in Qbasic mode  (Read 2709 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
File managing in Qbasic mode
« on: December 02, 2021, 09:10:29 pm »
Hi
here an example about how to manage if file exists or not in Qbasic way...
have a fun...
Code: QB64: [Select]
  1. 'this code shows a feature/bug of Qbasic inherited by QB64
  2. ' to duplicate experience please run this code into QB64 or Qbasic in DOSBOX
  3.  
  4. ' part 1 creates a file with a strange name so that it is difficult
  5. ' to have it as real file in your pc
  6. On Error GoTo ThisFile
  7. filename$ = "1a2b3c4d.$$$"
  8. Killing = 0
  9. 'testing if file exists
  10. Open filename$ For Input As #1
  11. Print "File exists and opened "; filename$
  12. TakeFile$ = Input$(LOF(1), # 1)
  13. Print TakeFile$
  14. If Killing = -1 Then Kill filename$: Print "Deleted file "; filename$
  15.  
  16. ThisFile:
  17. ' the file doesn't exist so it creates that and puts some data into file
  18. Txt$ = "hello world"
  19. Open filename$ For Binary As #1
  20. Put #1, , Txt$
  21. Print "file created "; filename$
  22. ' it says if the file must be delete at the end of code
  23. Killing = -1
  24. ' return on the linecode of error
in QB64 you need only of _Fileexists https://www.qb64.org/wiki/FILEEXISTS
Programming isn't difficult, only it's  consuming time and coffee

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: File managing in Qbasic mode
« Reply #1 on: December 03, 2021, 12:44:51 am »
OPEN file$ FOR BINARY AS #1
IF LOF(1) = 0 THEN NoExist = -1
CLOSE

IF NoExist THEN
    PRINT "File did not exist."
    KILL file$
END IF


The above also works well, generally speaking, as it's rather difficult to find 0-byte files on a drive, unless you just opened them yourself for some purpose (such as writing to them).  If YOU didn't make that 0-byte file, then should you really think of it as "existing"?  Isn't it the hard drive's equivalent of a blank string?

X$ = "" -- there's no X$.
LOF(1) = 0 -- there's no real file.

In the end, they're both just blank placeholders.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: File managing in Qbasic mode
« Reply #2 on: December 03, 2021, 06:54:05 pm »
Hi Steve
I like that practical approach to the problem: zero file aren't files it is a good Occam razor.
But this truth is truth until

-you are not searching files to be cancelled like the temp files of OS (do you remember the cut of unuseful files from Windows' folder done in Win 95 and Win ME? The more was 0 files)

-do you use all modes to create a file into QBasic  except the print INPUT mode .
Very glad for seeing  your opinion!
Programming isn't difficult, only it's  consuming time and coffee