Importing all the bytes of a file to unidimensional array

Started by Ishvaaag101, September 04, 2024, 06:43:23 AM

Previous topic - Next topic

Ishvaaag101

It is my first day doing something in BCX and I am trying to port a 46 lines a program in Freebasic, (have others in Mathcad and Fortran 95 -fully forgotten- for akin use) and I am meeting my first difficulty at line 12 at which I am importing all the bytes of a file to an unidimensional array i call T.

In BCX, starting when I open the file  for the import byte task

Open NameF For Binary As #1
L= LOF(NameF)
DIM DYNAMIC AS BYTE T[L-1]

and I don't know how to proceed, how less, efficiently.

In freebasic. starting with Open the five lines that follow, where T is accepted static, do.

Open NameF For Binary As #1
L= LOF(1)
Dim T(1 to L) As Ubyte
Get #1, , T()
Close #1

I understand that my meager readings of BCX have cut corners to infinite, and precisely for this any help will be greatly appreciated.

airr

Since a string is technically an array of chars, maybe this?


Dim NameF$, L

NameF$ = "File_1.bas"
L = Lof(NameF)

Dim T$*L

T$ = Loadfile$(NameF$)

For Int i = 0 To L -1
    Dim b As byte
    b = T$[i]
    Print b
Next
Pause


I'm not clear on what you need to do with the data, so this simple example may not suit your need....

AIR.

MrBcx

Ishvaag ... welcome ... here is your introductory free lesson  ;D



TYPE UniDimType                            ' First we'll define your "unidimensional array"
    MyBytes[20] AS BYTE
END TYPE

DIM Z AS UniDimType                        ' Now we'll create an instance of it

FOR INT i = 0 TO 19           
    Z.MyBytes[i] = i                       ' and fill it with "data"
NEXT

OPEN "MYDATA.FILE" FOR BINARY NEW AS #1    ' Create a file to save your data in
PUT #1, ADDRESSOF Z, SIZEOF(UniDimType)    ' Blast it in there
CLOSE #1                                   ' Close the file


DIM T AS UniDimType                        ' Create another instance just to prove things work
CLS                                        ' Clear the screen
OPEN "MYDATA.FILE" FOR BINARY READ AS #1   ' Open the file for binary reading
GET #1, ADDRESSOF T, SIZEOF(UniDimType)    ' read the entire disk file into your array
CLOSE #1                                   ' close the file

FOR INT i = 0 TO 19                        ' sanity check
    PRINT T.MyBytes[i]                     ' displlay the value of the bytes
NEXT

KILL "MYDATA.FILE"                         ' delete our test file
PAUSE                                      ' give us time to read the output 


MrBcx

I'm feeling generous today ... here's a version that reads the data file into a dynamic array.


TYPE UniDimType                            ' First we'll define your "unidimensional array"
    MyBytes[20] AS BYTE
END TYPE

DIM Z AS UniDimType                        ' Now we'll create an instance of it

FOR INT i = 0 TO 19
    Z.MyBytes[i] = i                       ' and fill it with "data"
NEXT

OPEN "MYDATA.FILE" FOR BINARY NEW AS #1    ' Create a file to save your data in
PUT #1, ADDRESSOF Z, SIZEOF(UniDimType)    ' Blast it in there
CLOSE #1                                   ' Close the file


CLS                                        ' Clear the screen

DIM ArraySize AS INT
ArraySize = LOF("MYDATA.FILE")
DIM DYNAMIC T[ArraySize] AS BYTE           

OPEN "MYDATA.FILE" FOR BINARY READ AS #1   ' Open the file for binary reading
GET #1, T, ArraySize                       ' read the entire disk file into your array
CLOSE #1                                   ' close the file

FOR INT i = 0 TO ArraySize-1               ' sanity check
    PRINT T[i]                             ' display the value of the bytes
NEXT

KILL "MYDATA.FILE"                         ' delete our test file
PAUSE                                      ' give us time to read the output


Ishvaaag101

#4
Thankyou both Airr and MrBCX. On my perusing your answers I think the 3d answer, by MrBCX is the closer to my ways, but I'll look both. I am not by trade an IT man, but an architect (building, construction) so my background is far from ideal to be quick to grasp programming (to me) novelties.

The program is just a method of encryption, really a working prototype, (have many) and any practical application would immediately demand professiomal work in every kind of application possible. The prototypes work well and fast in freebasic,  in windows provenly, and well, after paulatine progression (40 yrs) I take them as good, but at the moment the achievement is the algorithms showing being adaptable to several environments and encrypt/decrypt very strongly, with decryptions in brute force attacks by supercomputers requiring, well, very much ages of the universe. Embed in systems, organized comms, adapt to legal requirements is feasible but beyond the prototypes, that however are amenable to that, even in unusual ways.

Wanting the data in bytes and a vector is only because in over 90% of the prototypes I work in such layout, and rarely in visible way in 0s and 1s. So getting T holding the bytes surely will suit my needs.

As I said in my question, I am very grateful to both because the first steps in everything are the most difficult and you have been here wanting to help. Thank you very much for that.

airr

Hi, Ishvaaag101.

My example won't work if the data you're loading was saved in binary format (at least not without a lot of massaging).

Best to go with MrB's examples in that case.

AIR.

Ishvaaag101

#6
I understand that ordinary files saved by Windows Save are taken as binary. In fact I have not found exception when encrypting freebasic way every file in windows.(jpg, pdf, xls doc rar etc). Since I plan to implement both solutions you have gentle enough to teach me, I will see, mostly to force myself to learn more, that I see is worth.

I imagine that files holding data formatted by records neither be exception to my purpose. The encryption provides hypertext, and the hypertext decrypted devolves the exact same structure (file) it is in origin. Any file in windows is to my purpose a list of bytes, and I encrypt such list and decrypt in the decryption to produce the same bytes.

In the most elaborate prototypes the encryption packs the original name and extension in what to encrypt, and recovers them. In such cases you have an identical file to the original where put by the decryption. However such prototypes are few, so the practical way for the others (as stand) is making a rar of the file or files and encrypt the rar and you have the original names once decrypted. In any case, you will have the original file bytes either on its original name or one that you choose. Being comvenient to note that since embedding the name and rebuilding it at end even if taking a bit of code, are not expensive in run time, so to get it, it would be just another task over every chosen prototype not having the feature.

The most elaborate prototypes are fore sure between the safer, but are not for sure the faster nor the safest.

All these things, like as well, modifying the algorithm to suit chunks used for memory control, optimization,  preventiing exceptions, establishing ordered storage, implement legal wishes and doing it even without revealing the tech (that is feasible for embedded systems) and many more things are relegated to building the practical system.

This does not mean I have not met using chunks in the prototypes, as many other variations than using a T vector to encrypt; I simply have found by experience being the most efficient layout in many aspects, and particularly, for speed.


Ishvaaag101

#7
I have had first success in getting my goal in the (looks to me simpler) Airr way, program Air1.bas attached

I create a short file Viva.txt that I place in the same folder the .bas program is being written, then build and run. I have seen it to work in a 160K file; in practice I will need far bigger files that I assume can be treated this way or with small changes.

After getting the file in vector T I show the first 15 bytes of it

I attach .rar with the file Viva.txt and what of BCX

Putting the basic file here in the body of this entry (I don't know yet the interface) fails to report well the program so please resort to the attached files.

I have also tried the two entries by BCX but as of now I have not been successful, I will try more later today and more if necessary.



MrBcx

Here's a shorter version that also uses fewer variables:



CONST Fname = "Viva.txt"
DIM DYNAMIC T[LOF(Fname)] AS BYTE
T = CAST(PBYTE, LOADFILE$(Fname))
FOR INT i = 0 TO LOF(Fname)-1
    ? T[i];
NEXT
PAUSE


airr

I know you're showcasing how concise BCX code can be, MrB, but doesn't LOF get called multiple times in the loop? 

Could get expensive with a large file...something to keep in mind....

AIR.

MrBcx

Quote from: airr on September 06, 2024, 04:11:35 PM
I know you're showcasing how concise BCX code can be, MrB, but doesn't LOF get called multiple times in the loop? 

Could get expensive with a large file...something to keep in mind....

AIR.

You're exactly right ... I was waiting to see who would spot that first  ;)

jbk

did we not have a thread about for loops only evaluating the right hand expression only once ?
seems that PowerBasic and PureBasic do that and MrB agreed to do the same with BCX, no ?

airr

I wanna say,  it depends?

It's possible that the loop gets optimized by the C/C++ compiler in the case of BCX.

I learned a while ago not to count on that possibillity.  Cause you never really know for sure (unless your an ASM guru like Vortex, who would probably optimize the heck out of this manually!)

AIR.

Ishvaaag101

#13
Thank you very much everyone in the topic. I have got as well a correct solution with 2nd proposal of MrBCX extrincating the complexities (for me) of TYPE things. I have reduced it to reading the file to T and verifying it.

Refer to the .bas file in the attachment, since corrupted when posted inside the body of the message.

My problem is solved now with the help of all you; will follow next days the issue and entries to learn, and well, thank you all again.


Ishvaaag101

I have been trying the CAST option in the post of MrBCX and haven't been able to manage it to function. In all failures it shows error #2088: Lvalue required, the subsequent File not found I am surmising consequential to error #2088. I have trying giving L its value and as constant etc  and always that error #2088. In any case I want to get the size of the vector automatically and not after reading it in the screen from a LOF. I don't know if CAST is standard or need whatever. I 'll appreciate any advice to get this third option working.

Encryption or decryption can be relatively fast compared with any preprocessing, like input or output to T, so I plan to gauge the working options by the time it takes to get the bytes in T. For a proposal working prototype nothing needs to be an issue on that, but for some uses like in COMM it can turn to be critical.