QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: SirCrow on December 16, 2018, 09:38:51 am

Title: Ubuntu - Create Text File w/ Write Permission?
Post by: SirCrow on December 16, 2018, 09:38:51 am
In Linux, I have my prog. delete an existing text file, create a temp one and then rename it to the orig. one's name:
Code: QB64: [Select]
  1. OPEN "25_Pilots.tmp" FOR OUTPUT AS #2
  2. ...
  3. SHELL _HIDE _DONTWAIT "sudo rm -f Off_Pilots.txt"
  4. SHELL _HIDE _DONTWAIT "sudo mv 25_Pilots.tmp Off_Pilots.txt"
  5.  
Problem is, when the prog. later tries to write to the renamed file, it always fails.How do I create the text file with write permission?  Thanks!
Title: Re: Ubuntu - Create Text File w/ Write Permission?
Post by: FellippeHeitor on December 16, 2018, 09:41:51 am
When you open a file FOR OUTPUT you already reset its contents, so you don't need to use a temp file if you intend to overwrite the contents of an existing file. Just open Off_Pilots.txt FOR OUTPUT to begin with and write to it as you already do. No previous contents will remain that way.
Title: Re: Ubuntu - Create Text File w/ Write Permission?
Post by: SMcNeill on December 16, 2018, 09:50:34 am
You might need to simply:

OPEN original_file$ FOR OUTPUT AS #2
.
.
.Do stuff
.
.
CLOSE #2
KILL second_file$
RENAME original_file$ AS second_file$
OPEN second_file$ FOR APPEND AS #2

*********

(No need for the SHELL statements, as QB64 can both delete and rename files natively.)
Title: Re: Ubuntu - Create Text File w/ Write Permission?
Post by: SirCrow on December 16, 2018, 09:58:17 am
You might need to simply:

OPEN original_file$ FOR OUTPUT AS #2
.
.
.Do stuff
.
.
CLOSE #2
KILL second_file$
RENAME original_file$ AS second_file$
OPEN second_file$ FOR APPEND AS #2

*********

(No need for the SHELL statements, as QB64 can both delete and rename files natively.)

Thanks, I appreciate it.  But could someone please change KILL to something.....nicer?  After all these years?
Title: Re: Ubuntu - Create Text File w/ Write Permission?
Post by: SirCrow on December 16, 2018, 10:08:41 am
When you open a file FOR OUTPUT you already reset its contents, so you don't need to use a temp file if you intend to overwrite the contents of an existing file. Just open Off_Pilots.txt FOR OUTPUT to begin with and write to it as you already do. No previous contents will remain that way.

As usual, my own code confuses me, but I'm quite sure that I need the temp file at first, as Off_Pilots.txt already exists, and I need to place the end part of it into the temp file to preserve those lines of text.  After which, more lines will be appended to it.  Make sense?

Thanks, Fellippe, and Happy Anniversary!


Fuller version of my code:

Code: QB64: [Select]
  1.         OPEN "Off_Pilots.txt" FOR INPUT AS #1
  2.         OPEN "25_Pilots.tmp" FOR OUTPUT AS #2
  3.  
  4.         WHILE NOT EOF(1) 'Record the last 25 Pilots removed from duty
  5.             INPUT #1, P$: P = P + 1
  6.             IF P > PilCnt - 25 THEN PRINT #2, P$ 'Write the names to temp file
  7.         WEND: CLOSE #1, #2: P$ = "": P = 0
  8.  
  9.         SHELL _HIDE _DONTWAIT "sudo rm -f Off_Pilots.txt"
  10.         SHELL _HIDE _DONTWAIT "sudo mv 25_Pilots.tmp Off_Pilots.txt"
  11.