Jump to content

WordPad is messing the RTF file or bug in Autoit UDF?


Chuckero
 Share

Recommended Posts

Hello,

I'm trying to work with RTF files and I started with a very simple test, I got the example from the _GUICtrlRichEdit_StreamFromFile(), which is:

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $hGui, $iMsg, $idBtnNext, $iStep = 0, $idLblMsg, $hRichEdit
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    $idLblMsg = GUICtrlCreateLabel("", 10, 235, 300, 60)
    $idBtnNext = GUICtrlCreateButton("Next", 270, 310, 40, 30)
    GUISetState(@SW_SHOW)

    _GUICtrlRichEdit_SetText($hRichEdit, "First paragraph")
    While True
        $iMsg = GUIGetMsg()
        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                ; GUIDelete()   ; is OK too
                Exit
            Case $iMsg = $idBtnNext
                $iStep += 1
                Switch $iStep
                    Case 1
                        _GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")
                        GUICtrlSetData($idLblMsg, "Streamed to gcre.rtf")
                    Case 2
                        _GUICtrlRichEdit_SetText($hRichEdit, "")
                        _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf")
                        GUICtrlSetData($idLblMsg, "Streamed from gcre.rtf")
                    Case 3
                        _GUICtrlRichEdit_SetSel($hRichEdit, 2, 6)
                        _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf")
                        GUICtrlSetData($idLblMsg, "Replaced selection: an intentional mess!")
                        GUICtrlSetState($idBtnNext, $GUI_DISABLE)
                EndSwitch
        EndSelect
    WEnd
EndFunc   ;==>Example

So, on the first click on the "Next" button the scrip creates the file "gcre.rtf" in the Desktop. This is the content got from Notepad++:

image.png.6706dc6a12b9e401a3cdd75d9008c597.png

So far so good.

 

At this point I opened the file in WordPad, no changes, and just saved it. And in the Notepad I got:

image.png.b5e1bd5a377b45fdb325616907a53032.png

Note the NUL char added to end of the file, this mess up the file and the script can not read the file anymore. This problem happens in all files I tried to create using WordPad. If I use Word, its ok, but the file increase from 1kB to 43kB!
I'm using Windows and WordPad 10 version 1903 (OS Build 18362.1139).

Someone knows what is happening here?

 

Edited by Chuckero
Link to comment
Share on other sites

You could do something like this (untested) by replacing :

_GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")

with

Local $sTxt = FileRead(@DesktopDir & "\gcre.rtf")
If StringRight($sTxt,1) = chr(0) Then $sTxt = StringTrimRight($sTxt,1)
_GUICtrlRichEdit_StreamFromVar ($hRichEdit, $sTxt)

 

Link to comment
Share on other sites

Or simply use Word ;) (if possible). Size doesn't matter nowadays.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

5 hours ago, Chuckero said:

At this point I opened the file in WordPad, no changes, and just saved it

Notepad has the "save" option greyed out. You cannot save it with no changes. You must have done something to it in Notepad in order for it to "save".

If I do a "Save As" in Notepad I get an identical file;-

 

 

Screenshot.jpg

Phil Seakins

Link to comment
Share on other sites

On 11/12/2020 at 9:25 AM, Nine said:

You could do something like this (untested) by replacing :

_GUICtrlRichEdit_StreamToFile($hRichEdit, @DesktopDir & "\gcre.rtf")

with

Local $sTxt = FileRead(@DesktopDir & "\gcre.rtf")
If StringRight($sTxt,1) = chr(0) Then $sTxt = StringTrimRight($sTxt,1)
_GUICtrlRichEdit_StreamFromVar ($hRichEdit, $sTxt)

 

Good idea, but I tried and it doesn't work. Very weird.

This is what I got in the GUICtrlRichEdit:

image.png.ab332806889a664c6b0e1edc3b07d1ce.png

Link to comment
Share on other sites

On 11/12/2020 at 3:24 PM, pseakins said:

Notepad has the "save" option greyed out. You cannot save it with no changes. You must have done something to it in Notepad in order for it to "save".

If I do a "Save As" in Notepad I get an identical file;-

 

I'm not using NotePad I'm using WordPad. Even that, for me both software have the save option enabled, I can save the files directly with no changes in the content. And I can see the NUL char in NotePad++ and also in WinMerge.

This is another interesting situation: I used Notepad to test what you said, and in really NotePad can read the file and it has corrected the error created by WordPad when I saved the file.

Edited by Chuckero
Link to comment
Share on other sites

3 hours ago, Chuckero said:

I'm not using NotePad I'm using WordPad

My mistake. Well, anything can happen with Microsux products, I have gotten into a world of pain when my colleagues have used editors which have changed plain text Ascii files by adding headers and or trailers rendering them incompatible with my tools. It looks like your setup with Wordpad has it injecting it's own personality into your files.

Phil Seakins

Link to comment
Share on other sites

Ok found a way to work it out :

$hFile = FileOpen("gcre.rtf", $FO_BINARY)
            $dTxt = FileRead($hFile)
            FileClose($hFile)
            $sTxt = BinaryToString($dTxt)
            ConsoleWrite($sTxt & @CRLF)
            If StringRight($sTxt, 1) = Chr(0) Then
              ConsoleWrite("found char 0" & @CRLF)
              $sTxt = StringTrimRight($sTxt, 1)
            EndIf
            _GUICtrlRichEdit_StreamFromVar($hRichEdit, $sTxt)

Replace _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") with the above code.  :) Tested and working

Edited by Nine
Link to comment
Share on other sites

On 11/14/2020 at 8:20 AM, Nine said:

Replace _GUICtrlRichEdit_StreamFromFile($hRichEdit, @DesktopDir & "\gcre.rtf") with the above code.  :) Tested and working

YES!! Now it's working perfectly! Thanks!

What a workaround to be able to make this work 😉

Link to comment
Share on other sites

Glad you found a solution.

The problem is that RTF does have versions, and not all parsers are up to date or equally powerful. You will find that WordPad is really primitive and clumsy when it comes to editing RTF. 

 

Skysnake

Skysnake

Why is the snake in the sky?

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...