QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: PoliMi on March 09, 2019, 08:30:27 am

Title: Reading text file already open
Post by: PoliMi on March 09, 2019, 08:30:27 am
Is there a way to read without error a text file already open in another program?
Title: Re: Reading text file already open
Post by: TempodiBasic on March 09, 2019, 09:25:04 am
Hi PoliMi

IMHO if the file isn't LOCKED by the other program...
see here right info
http://qb64.org/wiki/LOCK (http://qb64.org/wiki/LOCK)
http://qb64.org/wiki/UNLOCK (http://qb64.org/wiki/UNLOCK)
http://qb64.org/wiki/OPEN (http://qb64.org/wiki/OPEN)
Title: Re: Reading text file already open
Post by: bplus on March 09, 2019, 09:31:37 am
As TempodiBasic notes there are exceptions but here is one successful try:

Here is an example:
  [ This attachment cannot be displayed inline in 'Print Page' view ]  


Welcome to the forum, PoliMi!
Title: Re: Reading text file already open
Post by: TempodiBasic on March 09, 2019, 10:39:25 am
Bplus
I can confirm your output and this logical error to loose information that are already in the TXT file.... (OUTPUT overwrites the file while APPEND just puts at the end of the file :-)  )
  [ This attachment cannot be displayed inline in 'Print Page' view ]  

Remember that
Quote
Modes INPUT, BINARY and RANDOM allow a file to be concurrently opened in a different mode and number.

and

Quote
File ACCESS and LOCK Permissions
ACCESS clause limits file access to READ, WRITE or READ WRITE on a network.
LOCK clause can specify SHARED or a LOCK READ or LOCK WRITE file lock in an OPEN statement working on a network.
A separate LOCK statement can lock or UNLOCK file access on a network using a format that can lock specific records.
If another process already has access to a specified file, program access is denied for that file OPEN access. A "Permission Denied" error 70 will be returned. A network program must be able to handle a denial of access error.

here screenshot with demo of LOCK keyword  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: Reading text file already open
Post by: bplus on March 09, 2019, 11:02:53 am
As far as I know, INPUT is the only file access that is guaranteed not to change the file when you open it in that mode to just read a file without messing it up.

Append: Oh, I see, more options to control access with ACCESS and LOCK, thanks TempodiBasic.
Title: Re: Reading text file already open
Post by: Pete on March 09, 2019, 12:28:10 pm
I used to make network programs in QBasic and I used SHARED along with LOCK and UNLOCK to regulate writing. I write an error trap routine to wait on files that were already open. SHARED alone worked perfectly to read files over a network that were never written to. In other words fixed data files that I did not want to put in the program, to save memory back in the day...

Well riddle me this. I thought for certain in QBasic I had to use SHARED to do two or more simultaneous reads (INPUT) of a sequential file (text file) in one program. In QB64, this is NOT necessary, as illustrated in this example belwo. Caution, it creates a file, but I doubt anyone has one named myfile-qb64-input-test.txt in their folder; but if you do, you've been warned. This code will overwrite it.

Code: QB64: [Select]
  1. OPEN "myfile-qb64-input-test.txt" FOR OUTPUT AS #1
  2. FOR h = 1 TO 7
  3.     PRINT #1, CHR$(64 + h)
  4.  
  5. ' Make a test file with letters A - G.
  6. OPEN "myfile-qb64-input-test.txt" FOR INPUT SHARED AS #1
  7.     i = i + 1
  8.     LINE INPUT #1, a$
  9.     PRINT a$
  10.     IF i = 2 THEN
  11.         PRINT "OK, let's open that file again! Press a key to begin...": SLEEP
  12.         OPEN "myfile-qb64-input-test.txt" FOR INPUT SHARED AS #2
  13.         DO UNTIL EOF(2)
  14.             j = j + 1
  15.             LINE INPUT #2, b$
  16.             PRINT b$
  17.             IF j = 5 THEN PRINT "Pause. Press any key to continue reading first file sequence...": SLEEP: EXIT DO
  18.         LOOP
  19.         CLOSE #2
  20.     END IF
  21. CLOSE #1, #2
  22. i = 0: j = 0
  23. PRINT: PRINT "Press a key to rerun now without SHARED...": SLEEP
  24. OPEN "myfile-qb64-input-test.txt" FOR INPUT AS #1
  25.     i = i + 1
  26.     LINE INPUT #1, a$
  27.     PRINT a$
  28.     IF i = 2 THEN
  29.         PRINT "OK, let's open that file again! Press a key to begin...": SLEEP
  30.         OPEN "myfile-qb64-input-test.txt" FOR INPUT AS #2
  31.         DO UNTIL EOF(2)
  32.             j = j + 1
  33.             LINE INPUT #2, b$
  34.             PRINT b$
  35.             IF j = 5 THEN PRINT "Pause. Press any key to continue reading first file sequence...": SLEEP: EXIT DO
  36.         LOOP
  37.         CLOSE #2
  38.     END IF
  39. CLOSE #1, #2
  40.  

Pete