Page 1 of 1

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

Posted: Sun May 03, 2020 12:21 am
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?

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

Posted: Sun May 03, 2020 12:37 am
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)

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

Posted: Sun May 03, 2020 12:52 am
by Omara1993
Hi there.
Wow, I was quite a ways off. Lol. Thanks for this!

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

Posted: Sun May 03, 2020 10:03 am
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.

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

Posted: Sun May 03, 2020 10:19 am
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