B4J Question Unreadble Characters

Discussion in 'B4J Questions' started by Pedro Caldeira, Apr 6, 2018.

  1. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Hello All,
    I am reading several text files into lists, processing the info and writing them again.
    One of the files, however has some special characters, tar i connot read correctly.
    Any ideias ?

    File:
    Code:
    :· 72033258 7203325704/04/2018
    :· 
    72033259 7203325704/04/2018
    :µ 
    72035960 7203596104/04/2018
    when I read the file, I get different chars in the lines and when I write the file I also get those char, instead of the origial ones.
    I have tried using lists and a TextReader, same results.
     
  2. KMatle

    KMatle Expert Licensed User

    Hard without seeing your code. I always use RandomAccessFile. You can write and read any object you like (lists, maps, texts, etc.). Here it is encrypted, too.

    Code:
    Dim raf As RandomAccessFile
    raf.Initialize(MyPath, 
    "config.dat"False)
    raf.WriteEncryptedObject(MyList, 
    "MySecretPW", raf.CurrentPosition)
    raf.WriteEncryptedObject(MyMap, 
    "MySecretPW", raf.CurrentPosition)
    raf.Close
     
    Pedro Caldeira likes this.
  3. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    My code is a simple file.readlist.
    however I am going to try your approach. thanks
     
  4. KMatle

    KMatle Expert Licensed User

    With it you can only write/read simple strings. Maybe this is the cause if e.g. a map is converted to a string or so.
     
    Pedro Caldeira likes this.
  5. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    so, to read a text file to a list but preserving its contents :

    Code:
    dim raf As RandomAccessFile
    raf.Initialize(somePath,someFile, 
    False)
    dim TextList as list = raf.ReadObject(raf.CurrentPosition)
    raf.Close
    the code gets stuck in the raf.readobject line
     
  6. Daestrum

    Daestrum Well-Known Member Licensed User

    Have you an example of the file you are trying to read, it could be just a simple encoding error that is preventing you from reading it.
     
    Pedro Caldeira likes this.
  7. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    Attached
     

    Attached Files:

  8. Daestrum

    Daestrum Well-Known Member Licensed User

    The only way I could get it to write the same chars was to read the original as windows-1251, which makes me think this file is from a terminal type device.
    Code:
    Dim tr As TextReader
     
    Dim li As List
     li.Initialize
     tr.Initialize2(
    File.OpenInput("c:/temp","cashier.txt"),"windows-1251")
     li = tr.ReadList
     tr.Close
     
    For Each l As String In li
      
    Log(l)
     
    Next
     
    Last edited: Apr 6, 2018
    OliverA, KMatle and Pedro Caldeira like this.
  9. KMatle

    KMatle Expert Licensed User

    Stuck means? Note: If you write encrypted, you need to read encrypted, too.
     
    Pedro Caldeira likes this.
  10. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    it is not encrypted, it just has some weird chars in the begining of the lines
     
  11. KMatle

    KMatle Expert Licensed User

    Please post the relevant code or an short example how you write and how you read it back (e.g. where is the text from?). I will take a look then.
     
    Pedro Caldeira likes this.
  12. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    No I can read it correctly using @Daestrum solution with "windows-1251" encoding
     
  13. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    However, no I notice something weird.
    I can read the file correctly.
    Then I filter some records and write the unfiltered ones to another list.
    when i log this last list lines, i get all the data in the log window, but when i write the list using a textwriter, i get an empty file.

    Code:
    Dim ListaCaixa as List
    Dim OutPutList as List

    dim tReader as TextReader
    tReader.Initialize2(
    File.OpenInput(WinRESTPath,"cashier.000"),"windows-1251")
    ListaCaixa = tReader.ReadList
    tReader.Close

    OutPutList.Clear
    For x=0 To ListaCaixa.Size -1
        
    If ListaCaixa.Get(x) <> UnwantedData Then
           OutPutList.Add(ListaCaixa.Get(x))
           
    log(ListaCaixa.Get(x)) ' Shows the correct data
        End If
    Next

    ' if I log the contents of OutPutList is correct

    dim tWriter as TextWriter
    tWriter.Initialize2(
    File.OpenOutput(WinRESTPath,"cashier.000",True),"windows-1251")
    tWriter.WriteList(OutPutList)
    tReader.Close
    cashier.000 is empty, size 0
     
  14. OliverA

    OliverA Well-Known Member Licensed User

    I'm pretty sure TextWriter is buffered, so until you call tWriter.Close, nothing may be written to disk (if all fits in the buffer). Alternatively, you can call tWriter.Flush to flush the buffers to disk (without closing the file).
     
    KMatle and Pedro Caldeira like this.
  15. Pedro Caldeira

    Pedro Caldeira Active Member Licensed User

    my bad !! I noticed that i was closing tReader instead of tWriter
     
    Last edited: Apr 6, 2018
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice