QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: MLambert on November 29, 2019, 12:44:19 am

Title: File buffers
Post by: MLambert on November 29, 2019, 12:44:19 am
Hi,

I have an input file with 2 fields A$ B$ .... A$ =1 and B$ =2

I want to write #1, A$,B$,    keeping the buffer open to write some more data.

I read the input file again and this time A$ = 3 and B$ =4.

I then write #1, A$,B$ and close off the write.

Will the new output file have 1 2 3 4 or 3 4 3 4 or 1 2 1 2 ?  I am trying to avoid saving fields etc..

Regards,

Mike
Title: Re: File buffers
Post by: SMcNeill on November 29, 2019, 02:08:39 am
These type questions are often just answered by trying them out for yourself.

OPEN “temp.txt” FOR OUTPUT AS #1
A$ = “1”
B$ = “2”
WRITE #1, A$
WRITE #1, B$
A$ = “3”
B$ = “4”
WRITE #1, A$
WRITE #1, B$
CLOSE

OPEN “temp.txt” FOR INPUT AS #1
DO UNTIL EOF(1)
    INPUT #1, C$
    PRINT C$
LOOP
CLOSE
Title: Re: File buffers
Post by: MLambert on November 30, 2019, 06:35:30 am
Thks ..
Title: Re: File buffers
Post by: TempodiBasic on November 30, 2019, 12:29:24 pm
  [ This attachment cannot be displayed inline in 'Print Page' view ]  
Title: Re: File buffers
Post by: Pete on November 30, 2019, 12:46:46 pm
It's also good to note that data is written via a file buffer, so if you check your data, by finding it and then viewing it in notepad, it might not have all the entries printed to it yet. A CLOSE or something to end the program, other than probably an error message, is needed to write the remaining buffered data to t he file.

I hope I got that all correct, because I'm writing this from my 30 year memory... which is in drastic need of an upgrade!!!

Fred, ah I mean Pete

PS: I put "probably" about the error situation. Maybe it dumps the buffer data to the file, maybe it doesn't. ???
Title: Re: File buffers
Post by: Petr on November 30, 2019, 01:12:29 pm
My example how read records from text file:

Code: QB64: [Select]
  1. 'EVERY NEW FILE OPENING = READING IT FROM BEGIN.
  2. 'EXAMPLE HOW READ CONCRETE RECORD (1 record = 1 row) FROM TEXT FILE:
  3.  
  4. 'first create text file, which contains 10 records:
  5.  
  6. OPEN "TextFile.txt" FOR OUTPUT AS #1
  7. FOR R = 1 TO 10
  8.     s$ = "This is record in file TextFile.txt number:" + STR$(R)
  9.     PRINT #1, s$
  10.  
  11. 'How read concrete record from this file:
  12. INPUT "Input record number:"; rec
  13. OPEN "TextFile.txt" FOR INPUT AS #1
  14.     LINE INPUT #1, r$
  15.     row = row + 1
  16.     IF row = rec THEN rec$ = r$
  17. IF LEN(rec$) THEN PRINT rec$ ELSE PRINT "Record not found"
  18. rec$ = ""
  19. PRINT "Press key..."
  20.  
  21. 'or second example: more records in one row:
  22. 'create text file contains 10 rows and values 1 to 10, 11 to 20.... 99 to 100
  23.  
  24. OPEN "TextFile.txt" FOR OUTPUT AS #1
  25. rec = 0
  26. s$ = ""
  27. rec = 1
  28. FOR R = 1 TO 10
  29.     FOR onRow = 1 TO 10
  30.         IF onRow < 10 THEN separator$ = ", " ELSE separator$ = ""
  31.         s$ = s$ + "Record:" + STR$(rec) + separator$
  32.         rec = rec + 1
  33.     NEXT
  34.     PRINT #1, s$
  35.     s$ = ""
  36. rec = 0
  37.  
  38. INPUT "Input record number:"; rec
  39. OPEN "TextFile.txt" FOR INPUT AS #1
  40.     INPUT #1, r$ 'for values on row use INPUT
  41.     FileRec = FileRec + 1
  42.     IF FileRec = rec THEN rec$ = r$
  43. IF LEN(rec$) THEN PRINT rec$ ELSE PRINT "Record not found"
  44.  
Title: Re: File buffers
Post by: SMcNeill on November 30, 2019, 01:23:21 pm
It's also good to note that data is written via a file buffer, so if you check your data, by finding it and then viewing it in notepad, it might not have all the entries printed to it yet. A CLOSE or something to end the program, other than probably an error message, is needed to write the remaining buffered data to t he file.

I hope I got that all correct, because I'm writing this from my 30 year memory... which is in drastic need of an upgrade!!!

Fred, ah I mean Pete

PS: I put "probably" about the error situation. Maybe it dumps the buffer data to the file, maybe it doesn't. ???

QB64 reads and writes byte by byte, without any use of file buffering, if you OPEN for OUTPUT, APPEND, or INPUT. 

Files opened for RANDOM, or for BINARY *do* use file buffering, and read large chunks of data at a time.

It's this difference in method access which makes OPEN file$ FOR INPUT sooooo much slower than OPEN file$ FOR BINARY, and why I added LINE INPUT as a valid command for use with BINARY mode for us.  ;)
Title: Re: File buffers
Post by: Pete on November 30, 2019, 02:00:50 pm
That jogs my memory a bit. 30 years back, I used RANDOM access for patient files. I vaguely remember breaking into Notepad to view some data I knew I had written, and nearly soiled myself when I discovered it was not there!Lucky for me, I completed the process in my program, and the buffer contents was placed in the file. That's when I figured out the buffer part, but now I have an answer on the speed difference between the two, and that's interesting. I haven't played around with files for a few years, but I did start using that LINE INPUT with BINARY access the minute it hit the shelves. That was a stellar QB64 project addition!

Pete