Author Topic: Having trouble with PUT  (Read 4104 times)

0 Members and 1 Guest are viewing this topic.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Having trouble with PUT
« on: December 09, 2020, 01:31:14 am »
Ok the wiki says that the whole array can be written to a file like

 DIM string2(16384) AS STRING
****
PUT #1, , string2() 'file write

I get the error message:
"cannot pass array of variable- length strings on current line"

but there is no such error for :

GET #1, , string2() 'file read 

And here is where it gets ugly

if I DIM it like:

DIM string2(16384) AS STRING * 1

then the
"cannot pass array of variable- length strings on current line"

goes away  - but instead I now get another error:

"C++ compilation failed"
****
From the wiki you can see how GET and PUT are used

Quote
DIM SHARED array(100) AS INTEGER

FOR i = 1 TO 100
  array(i) = i
NEXT
showme  'display array contents

OPEN "BINFILE.BIN" FOR BINARY AS #1

PUT #1, , array()

ERASE array 'clear element values from array and display empty
showme
CLOSE #1

OPEN "BINFILE.BIN" FOR BINARY AS #2
GET #2, , array()
CLOSE #2
showme  'display array after transfer from file


I can't get it to work

ive never had to do
DIM string2(16384) AS STRING * 1
and it seems to bork the complier

Actually I've never had to use an array to do file read / writes. There is absolutely no point in doing so. I sure miss the DOS interrupts at this point :(

I have always found that
DIM string1 AS STRING * 16384
was sufficient for file read / writes, because that DIM actually reserves a block of memory. You can see the block in the EXE file if you look at it with a hex editor. 

****
I wish QB64 had ReadFile and WriteFile

DIM string1 AS STRING * 16384

WriteFile #1, string1, nbytes

ReadFile #1, string1, nbytes

where nbytes is the number of bytes to read or write.
string1 is a FIXED length string

string1 is simply a file buffer in this case. You certainly don't need the overcomplication of an array or the bizarre implications of a variable length string to do that.
« Last Edit: December 09, 2020, 04:14:29 am by NOVARSEG »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Having trouble with PUT
« Reply #1 on: December 09, 2020, 09:11:13 am »
@NOVARSEG ,

Why are you using an array? They aren't necessary in GET and PUT file operations. Also, why an array of 16384 strings? Why such a large and arbitrary number? Can you post your code and the file you are trying to read? If you post your code, please use the code block option so it is easier to read and copy to QB64. Also, QB64 does have ReadFile and WriteFile, provided you use the WinAPI. I wouldn't use it to read a file in QB64 since there are already so many good ways to do it with the built-in functions and keywords. I would just recommend looking at other code on the forum and find where someone is reading from or writing to a file, either as an entire string or as a line-by-line ordeal.
« Last Edit: December 09, 2020, 09:31:40 am by SpriggsySpriggs »
Shuwatch!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Having trouble with PUT
« Reply #2 on: December 09, 2020, 11:07:57 am »
OK THANKS

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Having trouble with PUT
« Reply #3 on: December 09, 2020, 11:14:48 am »
Should work with number Types and I am little surprised if it doesn't work with Fixed string. It makes sense to me why variable strings wont work. I wonder if you try something larger than 1 for a fixed string, because integers are 2 (the example from Wiki) and 1 can be done with single buffer albeit not in pieces of 1 but you have MID$, LEFT$, RIGHT$ and ASC to get that.

@NOVARSEG  did you get your IDE back?
« Last Edit: December 09, 2020, 11:24:41 am by bplus »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Having trouble with PUT
« Reply #4 on: December 09, 2020, 11:38:16 am »
Should work with number Types and I am little surprised if it doesn't work with Fixed string.

I'm pretty sure it does work with fixed-length strings. I think it was because of the wonky way he is using them. If you are going to do 16384 strings with lengths of 1 then you might as well just use an array of 16384 _BYTEs.
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Having trouble with PUT
« Reply #5 on: December 09, 2020, 12:45:44 pm »
Yeah bytes would be better (if works for sure) because numbers processing usually faster than string.

Confirmed fixed strings of any length dont work, get compile error with Wiki example. Seems to me this came up before when trying to copy Fixed string arrays with _MEM stuff, at least by the method I was using that worked on number types.

PS the quote in OP is missing the showme subroutine.
« Last Edit: December 09, 2020, 01:21:36 pm by bplus »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Having trouble with PUT
« Reply #6 on: December 09, 2020, 02:33:08 pm »
Yeah bytes would be better (if works for sure) because numbers processing usually faster than string.

DIM a(1000) AS _UNSIGNED _BYTE
DIM b(1000) AS _UNSIGNED _BYTE

OPEN "temp.txt" FOR BINARY AS #1

FOR i = 1 TO 1000
    a(i) = 65 + INT(RND * 26)
NEXT

PUT #1, 1, a()
GET #1, 1, b()
FOR i = 1 TO 10
    PRINT i, CHR$(a(i)), CHR$(b(i))
NEXT
CLOSE

Bytes work without a problem.  String * 1 should work, but there's a glitch in QB64 itself somewhere.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Having trouble with PUT
« Reply #7 on: December 09, 2020, 09:52:31 pm »
I had to use a variable length string to get my file encryptor code working. Was trying to use a fixed length string like

DIM string2 AS STRING * 16484
GET #1, ,string2

above reads 16384 bytes from file to string2.  Now if you do this:

string2 = space$(100)
GET #1, ,string2
still reads 16384 bytes. So the DIM is changed to

DIM string2 AS STRING
now
string2 = space$(100)
GET #1, ,string2

reads 100 bytes.
****
Still  want to use a fixed length string as a file buffer but that might require Windows API as suggested.


« Last Edit: December 09, 2020, 09:54:13 pm by NOVARSEG »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Having trouble with PUT
« Reply #8 on: December 10, 2020, 08:12:20 am »
Just fiddle around with it until you get something working. I believe in you.

Also, I wasn't suggesting using Windows API. In fact, I advised against it. You said you would rather use ReadFile and WriteFile which I told you are WinAPI functions that can be used in QB64. I suggested that you just check the forum for code using the built-in QB64 keywords to read from and write to a file instead because they would be simpler. The WinAPI functions for file operations are very touchy and require much knowledge of security rights and file attributions. It can be done but I strongly advise you to not waste time digging into that.
« Last Edit: December 10, 2020, 08:24:46 am by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Having trouble with PUT
« Reply #9 on: December 10, 2020, 09:57:11 am »
I had to use a variable length string to get my file encryptor code working. Was trying to use a fixed length string like

DIM string2 AS STRING * 16484
GET #1, ,string2

above reads 16384 bytes from file to string2.  Now if you do this:

string2 = space$(100)
GET #1, ,string2
still reads 16384 bytes. So the DIM is changed to

DIM string2 AS STRING
now
string2 = space$(100)
GET #1, ,string2

reads 100 bytes.
****
Still  want to use a fixed length string as a file buffer but that might require Windows API as suggested.

Standard way is to read the file length after you open the file FOR BINARY AS #1
buff$ = SPACE$(LOF(1))
GET #1, ,buff$

The file is now in buff$.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Having trouble with PUT
« Reply #10 on: December 10, 2020, 12:50:10 pm »
Standard way is to read the file length after you open the file FOR BINARY AS #1
buff$ = SPACE$(LOF(1))
GET #1, ,buff$

The file is now in buff$.

Precisely the way I read my files. Works every time and is quite fast.
Shuwatch!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: Having trouble with PUT
« Reply #11 on: December 11, 2020, 10:28:49 am »
Does that really develop muscle?   Sure faster than steroids.
It works better if you plug it in.