carl1905

Filewrite append mode?

6 posts in this topic

This code will read text data from txt file. However, if I use '$Newtext &= $convert' then $convert is saved as strings, not hex value.

For example, if $convert = 0x300020FF and $bNewText = Test then the result is 0x333030303230464654657374. (Wrong)

What I want is 0x300020FF54657374. How can I modify my script?

For $i = $y-1 To Ubound($split) - 1

          $convert = "0x" & $split[$i][0]
          $Newtext &= $convert
          $NewLen_hex = BinaryLen ($convert)

          $i=$i+1
          $split[$i][1] = $split[$i][0]
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF)
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF)
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR)
          $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS)

          if $i = Ubound($split) - 1 Then
             $bNewText &= Chr(0)
          EndIf

          $Newtext &= $bNewText
          $NewLen = $NewLen_hex + BinaryLen ($bNewText)
          Next
          
          (...)
          (...)
          
          $Newfile = BinaryMid($Newfile_header,1) & BinaryMid($Newtext,1)
          $hNewfile = FileOpen ("NEW_"&$Name, 2+16)
          FileWrite ($hNewfile, $Newfile)
          FileClose ($hNewfile)
          TrayTip ("Importer", "Finish!", 3)
          sleep (3000)

 

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

8 minutes ago, Jos said:

It would help when you show a snippet that shows the issue and we can run.

Jos

Ok. My code makes a new binary file from original file and txt.

However, I'm major in atomic physics and I'm not sure how can I show you my script as a snippet, so I just post my whole code and sample file.

#include <File.au3>
#include <Winapi.au3>
#include <Binary.au3>
#include <String.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

Global Const $CP_SHIFT_JIS = 932

Func _ArrayAddColumns(ByRef $aArr, $iNumColToAdd = 1)
    If IsArray($aArr) = 0 Then Return SetError(1, 0, -1) ; Filter out non-array
    If $iNumColToAdd < 1 Then Return SetError(2, 0, -1) ; $iNumColToAdd must be greater than zero to add a column.
    If UBound($aArr, 0) > 2 Then Return SetError(3, 0, -1) ; Only allows a 1d or 2d array pass this line.

    If UBound($aArr, 0) = 1 Then ; ====== For 1d array ========
        Local $aRet[UBound($aArr)][$iNumColToAdd + 1] ; Create new 2d array.
        For $r = 0 To UBound($aArr) - 1
            $aRet[$r][0] = $aArr[$r]
        Next
    Else ; ======= For 2d array ============
        Local $aRet = $aArr ; So that ByRef $aArr is not altered outside of function.
        ReDim $aRet[UBound($aRet)][UBound($aRet, 2) + $iNumColToAdd] ; ReDim 2d array only.
    EndIf

    Return $aRet
EndFunc   ;==>_ArrayAddColumns

Dim $NEWdata
$TxtPath = FileOpenDialog("Select the TXT file", @ScriptDir, "text files (*.txt)",1)
If @error = 1 Then Exit
_FileReadToArray($TxtPath,$NEWdata)
$Name = StringTrimRight(CompGetFileName($TxtPath),4)
$File = FileOpen ($Name, 0+16)

If $File = -1 Then
MsgBox(0,"Error!","Can't open "&$Name&" file.")
Exit
EndIf

FileSetPos($File,4,0)
$table_off = Dec(Hex(FileRead($File,2))) ;  0x1E2C Read
$Base = $table_off * 2                   ;  0x3C58

FileSetPos($File,$Base,0)   ;   0x3C58
$table_size = Dec(Hex(FileRead($File,4)))   ;   0x300. From 0x3C58 + 0x300 = 0x3F58
$table_end = $Base + $table_size            ; table_end is exactly same as starting point of string tbl.
$Offset = $table_end                        ; let table_end as offset

FileSetPos($File,0,0)
$Newfile_header = FileRead($File,$table_end) ; read *.FPS4 from 0 to offset = Making header file
FileSetPos($File,$Base,0)

$Newtext = ""
$Newfile = ""

$j = 1
Dim $Text
Do
       $pos = FileGetPos($File)
       $split = StringSplit($NEWdata[$j],"{}")
       $split = _ArrayAddColumns($split)
       ;_ArrayDisplay($split)

       if StringInStr($NEWdata[$j], "{") = 1 Then
          $y = 3

          For $i = $y-1 To Ubound($split) - 1

          $convert = "0x" & $split[$i][0]
          $Newtext &= $convert
          $NewLen_hex = BinaryLen ($convert)

          $i=$i+1
          $split[$i][1] = $split[$i][0]
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF)
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF)
          $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR)
          $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS)

          if $i = Ubound($split) - 1 Then
             $bNewText &= Chr(0)
          EndIf

          $Newtext &= $bNewText
          $NewLen = $NewLen_hex + BinaryLen ($bNewText)
          Next

       Else
          $y = 1

          For $i = $y-1 To Ubound($split) - 1

             if $split[$i][0] = 1 Then

             $i=1
             $split[$i][0] = StringRegExpReplace($split[$i][0], "<cf>", @CRLF)
             $split[$i][0] = StringRegExpReplace($split[$i][0], "<lf>", @LF)
             $split[$i][0] = StringRegExpReplace($split[$i][0], "<cr>", @CR)
             $bNewText = _WinAPI_WideCharToMultiByte($split[$i][0], $CP_SHIFT_JIS) & Chr(0)
             $Newtext &= $bNewText

             $NewLen = BinaryLen ($bNewText)

             Else

             For $i = $y - 1 To Ubound($split) - 1

             if $i <> 0 Then
             $convert = "0x" & $split[$i][0]
             $Newtext &= $convert
             $NewLen_hex = BinaryLen ($convert)
             EndIf

             $i=$i+1
             $split[$i][1] = $split[$i][0]
             $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF)
             $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF)
             $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR)
             $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS)

             if $i = Ubound($split) - 1 Then
                $bNewText &= Chr(0)
                $Newtext &= $bNewText
                $NewLen = $NewLen_hex + BinaryLen ($bNewText)
             EndIf

             Next


             EndIf

          Next

       EndIf

       $New_Offset = _BinaryReverse($table_size + $Offset - $table_end)
       $Newfile_header = _BinaryPoke($Newfile_header,$pos+1,$New_Offset,"dword")
       $Offset += $NewLen
       $pos = $pos + 4
       FileSetPos($File,$pos,0)
       $j += 1
       $convert = ""
       $bNewText = ""
Until $pos = $table_end

$Newfile = BinaryMid($Newfile_header,1) & BinaryMid($Newtext,1)
$hNewfile = FileOpen ("NEW_"&$Name, 2+16)
FileWrite ($hNewfile, $Newfile)
FileClose ($hNewfile)
TrayTip ("Importer", "Finish!", 3)
sleep (3000)

Func __HexToString($sString)
    If StringLeft($sString, 2) <> "0x" Then $sString = "0x" & $sString
    Return BinaryToString($sString)
EndFunc

Func CompGetFileName($Path)
If StringLen($Path) < 4 Then Return -1
$ret = StringSplit($Path,"\",2)
If IsArray($ret) Then
Return $ret[UBound($ret)-1]
EndIf
If @error Then Return -1
EndFunc

 

sample.zip

Edited by carl1905

Share this post


Link to post
Share on other sites

I found that only write mode saves "1F010006300020FF" as 0x1F010006300020FF in binary file.

However, my script use filewrite at the end of code. I don't have idea how to write "1F010006300020FF" in advance and then &= $Newtext.

Share this post


Link to post
Share on other sites

Uhm.. Does anyone have an idea on it?

Share this post


Link to post
Share on other sites

Still think you should minimize your script to a few lines that simply show the issue you have. Shouldn't be that hard. ;)

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • wimhek
      By wimhek
      Is it possible , and how can I read and write txt files from Icloud (apple service) ?
      Let me try to explain my application.
      On my Ipad and Iphone I create txt files. On my windows computer it is possible to read and modify these files manually, by logging in on www.icloud.com.
      What I want to make is an auto-it script who reads the txt file and create an new txt file on www.icloud.com,  so I can acces these on my ipad and/or phone.
       
      Thank you.
    • Skysnake
      By Skysnake
      Hi there
      I have searched and found many posts on READING CSV.  What I need to do is WRITE CSV.
      Typically I have a Listview, I use @guinness's excellent  _GUICtrlListView_CreateArray to read that LV to Array. Currently I use _FileWriteFromArray to write the output.  It is fast and it is easy.
      The benefits of using these Array functions are their inherit flexibility.  The code is portable.  Plug it into LV report and you can write to file instantly.  No formatting issues.
      To be sure, my problem is not with the Listview or Array.  My problem is with the file write to CSV: it should be fast, accurate and efficient.
      Part of the problem is Microsoft Excel's insistence that my CSVs are not what they appear to be...
       
      I am inspired by the simplicity of SQLITE3.EXE's dot command options:
      .headers on .mode csv .once output.csv  
      Problem is that I want to export the text packed in characters, such as ' " ' with fields delimited by another character, such as ' , '.
      Current method using _FileWriteFromArray outputs data like this
      12-04-2016| 0.00| 131.00|131|Description 12-04-2016| 0.00| 132.00|132|Description 132 rece 12-04-2016| 998.00| 0.00|998|Description Receipt 12-04-2016| 0.00| 900.00|900|Description What I want to achieve is this:
      "12-04-2016"," 0.00"," 131.00","131","Description" "12-04-2016"," 0.00"," 132.00","132","Description 132 rece" "12-04-2016"," 998.00"," 0.00","998","Description Receipt" "12-04-2016"," 0.00"," 900.00","900","Description" I have done this. The long way. I have written the output one $aResult[$i][1] item at a time, spaced with the $text & $field markers.  I have placed this in an array with additional columns just to fit in the formatting. I have also played with the idea of inserting the array into a sqlite db, then run sqlite3's dot commands on that.
      Is there a different method?  Is there an efficient method to specify FileWriteFromArraytoCSV?  Does anyone have ideas?  I am sure I will know how to code this, I just need advice on the most efficient way of going about it. If I missed any threads, please enlighten  me.
      Thanks for reading
    • cherrylatte
      By cherrylatte
      Hello,
      I would like to make a script like the below
      open the browser > copy the url in the browser's address bar > save it in a text file
      I have no clue on how to make this
      I'd appreciate for any help
    • oasis375
      By oasis375
      This doesn't work, it deletes (overwrites) file contents:
      $hFile = FileOpen("file.txt", 2) $content1 = FileRead($hFile) $content2 = StringReplace($content1, "old", "new") FileWrite($hFile, $content2) FileClose($hFile) Why? Instead, I have to use this workaround:
      $hFile = FileOpen("file.txt") $content1 = FileRead($hFile) $content2 = StringReplace($content1, "old", "new") FileClose($hFile) $hFile = FileOpen("file.txt",2) FileWrite($hFile, $content2) FileClose($hFile) That's it, opening, closing and reopening the file.
      Function reference says: FileWrite. "Write text/data to the end of a previously opened file." (Does it mean "append"?).
      Remarks: "The file must be opened in write mode or the FileWrite() command will fail."
      But what write mode? $FO_APPEND (1) = Write mode (append to end of file). $FO_OVERWRITE (2) = Write mode (erase previous contents).
       
    • TheDcoder
      By TheDcoder
      Hello, I have a simple task today, I am sure that I made a similar post a long time ago... I want trim (delete or remove or wipe) the contents of a file (text or binary) WITHOUT storing the contents of the file anywhere in the script. Here is a text file for the purpose of experimenting: (contents of text.txt)
      1234567890 The task is simple, remove "456" from the contents of text.txt WITHOUT storing the contents anywhere! I wanted to post some code but it seems impossible to provide any relevant code this time...
       
      Good luck with the challenge! TD