Attempts to read a file into a string aren't fully working

Just starting out? Need help? Post your questions and find answers here.
Omara1993
New User
New User
Posts: 3
Joined: Sat May 02, 2020 11:56 pm

Attempts to read a file into a string aren't fully working

Post by Omara1993 »

Hello all.
I have function that is supposed to make my life a little easier. It reads a file and returns it as a string.
It works out great if you give it a text file, though I might not always handle text files. This is where the problem is. I'm actually trying to revise my sound encryption system, this time, I'm encrypting them in memory, rather than to disk, such that they can be written to a pack file, to eliminate the extra step of opening that encrypted file on disk then putting it into the pack file.
With ogg vorbis files in particular, it peeks the first 4 or 6 bytes to string, then promptly stops, which gives me an incomplete result and i can't use that with the AES encoder.
Here is what I have as of now, just the file reading section.

Code: Select all

Procedure.s read_file(inpt.s)
ReadFile(0, inpt)
*buffer=AllocateMemory(Lof(0))
ReadData(0, *buffer, Lof(0))
If Eof(0)=1
CloseFile(0)  
EndIf
f.s=PeekS(*buffer, MemorySize(*buffer), #PB_Ascii)
            Debug("Returning "+Str(StringByteLength(f)))
      FreeMemory(*buffer)
            ProcedureReturn f
             EndProcedure
As I say, this works almost perfectly if you give it text, which is great if I was just handling text.
Anyone have any ideas?
User avatar
Olliv
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Sep 22, 2009 10:41 pm

Re: Attempts to read a file into a string aren't fully worki

Post by Olliv »

Code: Select all

Procedure FileData(inpt.s)
Define Size = FileSize(inpt)
If Size > 0
 Define File = ReadFile(#PB_Any, inpt)
 Define *Buffer = AllocateMemory(Size)
 ReadData(File, *Buffer, Size)
 CloseFile(File)
EndIf
ProcedureReturn *Buffer
EndProcedure

Structure Bytes
 B.B[1 << 31 - 1]
EndStructure

Define *X.Bytes = FileData(myFile$)

For i = 0 To MemorySize(*X) - 1
 Debug Str(i) + " : " + Str(*X\B[i] ) + " == " + Chr(*X\B[i] )
Next

FreeMemory(*X)
Omara1993
New User
New User
Posts: 3
Joined: Sat May 02, 2020 11:56 pm

Re: Attempts to read a file into a string aren't fully worki

Post by Omara1993 »

Hi there.
Wow, I was quite a ways off. Lol. Thanks for this!
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Attempts to read a file into a string aren't fully worki

Post by infratec »

If it is a text file:

Code: Select all

Procedure.s read_file(inpt.s)
  
  Protected File.i, Format.i, Text$
  
  File = ReadFile(#PB_Any, inpt)
  If File
    Format = ReadStringFormat(File)
    Text$ = ReadString(File, Format|#PB_File_IgnoreEOL)
    CloseFile(File)
  EndIf
  
  ProcedureReturn Text$
  
EndProcedure
No need for a buffer.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Attempts to read a file into a string aren't fully worki

Post by mk-soft »

I like this ...

Code: Select all


Procedure ReadTextFile(FileName.s, List Result.s())
  Protected File.i, Format.i
  
  ClearList(Result())
  File = ReadFile(#PB_Any, FileName)
  If File
    Format = ReadStringFormat(File)
    While Not Eof(File)
      AddElement(Result())
      Result() = ReadString(File, Format)
    Wend
    CloseFile(File)
  EndIf
  ProcedureReturn ListSize(Result())
EndProcedure

; ----

Define NewList MyText.s()
Define TextFile.s = OpenFileRequester("Open", "", "", 0)

If TextFile
  r1 = ReadTextFile(TextFile, MyText())
  Debug "Count of Lines: " + r1
  ForEach MyText()
    Debug MyText()
  Next
EndIf

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply