Author Topic: Ubuntu - Create Text File w/ Write Permission?  (Read 2736 times)

0 Members and 1 Guest are viewing this topic.

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Ubuntu - Create Text File w/ Write Permission?
« 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!
I may not always finish what I've started....

FellippeHeitor

  • Guest
Re: Ubuntu - Create Text File w/ Write Permission?
« Reply #1 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Ubuntu - Create Text File w/ Write Permission?
« Reply #2 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.)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Ubuntu - Create Text File w/ Write Permission?
« Reply #3 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?
I may not always finish what I've started....

Offline SirCrow

  • Forum Regular
  • Posts: 144
    • View Profile
Re: Ubuntu - Create Text File w/ Write Permission?
« Reply #4 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.  
« Last Edit: December 16, 2018, 10:13:11 am by SirCrow »
I may not always finish what I've started....