Jump to content

Recommended Posts

Posted
#include <File.au3>
#include <FileConstants.au3>

Local $sTestfile = "_FileWriteToLine.txt"
Local $hTestfile = FileOpen($sTestfile, $FO_APPEND)
FileWriteLine($hTestfile, "Line 1")
FileWriteLine($hTestfile, "Line 2")
FileWriteLine($hTestfile, "Line 3")
FileClose($hTestfile)
_FileWriteToLine("_FileWriteToLine.txt", 2, "Overwritten", True)

The above example code first creates a text file that has four lines after FileClose:

  1. Line 1
  2. Line 2
  3. Line 3
  4. And an empty line.

After _FileWriteToLine only three lines are left:

  1. Line 1
  2. Overwritten
  3. Line 3

The empty line has been removed by the function _FileWriteToLine!

  • Moderators
Posted

Moved to the appropriate forum.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

@DonChunior
If you change the line 

FileWriteLine($hTestfile, "Line 3")

to 

FileWriteLine($hTestfile, "Line 3" & @CRLF)

the blank line at the end of the file won't be added, since FileWriteLine(), as it is stated in the Help file:

Quote

Parameters

filehandle/filename: The handle of a file, as returned by a previous call to FileOpen(). Alternatively, you may use a string filename as the first parameter.
line                            : The line of text to write to the text file. If the line does NOT end in @CR or @LF then a DOS linefeed (@CRLF) will be automatically added.

And, about the "magic" disappearing of the last line in the file, I think that's the effect of the _FileWriteToLine(), which is written like so:
 

; #FUNCTION# ====================================================================================================================
; Author ........: cdkid
; Modified.......: partypooper, MrCreatoR
; ===============================================================================================================================
Func _FileWriteToLine($sFilePath, $iLine, $sText, $bOverWrite = False)
    If $iLine <= 0 Then Return SetError(4, 0, 0)
    If Not IsString($sText) Then
        $sText = String($sText)
        If $sText = "" Then Return SetError(6, 0, 0)
    EndIf
    If $bOverWrite = Default Then $bOverWrite = False
    If Not (IsBool($bOverWrite) Or $bOverWrite = 0 Or $bOverWrite = 1) Then Return SetError(5, 0, 0) ; For old versions.
    If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)

    Local $aArray = FileReadToArray($sFilePath)
    Local $iUBound = UBound($aArray) - 1
    If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0)

    Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE)
    If $hFileOpen = -1 Then Return SetError(3, 0, 0)

    Local $sData = ""
    $iLine -= 1 ; Now the array is 0-based, so reduce the line number by 1.
    For $i = 0 To $iUBound
        If $i = $iLine Then
            If $bOverWrite Then
                If $sText Then $sData &= $sText & @CRLF
            Else
                $sData &= $sText & @CRLF & $aArray[$i] & @CRLF
            EndIf
        ElseIf $i < $iUBound Then
            $sData &= $aArray[$i] & @CRLF
        ElseIf $i = $iUBound Then
            $sData &= $aArray[$i]
        EndIf
    Next

    FileWrite($hFileOpen, $sData)
    FileClose($hFileOpen)
    Return 1
EndFunc   ;==>_FileWriteToLine

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Posted

Hi Francesco,

thanks for your reply.

Thus, the problem seems to be that the _FileWriteToLine function completely rewrites the file and does not just edit the corresponding line!
Of course it is a matter of opinion whether this is a bug or not.

Posted
3 hours ago, DonChunior said:

Of course it is a matter of opinion whether this is a bug or not.

Not a bug, works that way by design.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

  • Moderators
Posted

FrancescoDiMuro,

It appears that your AutoIt version is not up to date - the function currently reads (v3.3.14.5):

; #FUNCTION# ====================================================================================================================
; Author ........: cdkid
; Modified.......: partypooper, MrCreatoR, Melba23
; ===============================================================================================================================
Func _FileWriteToLine($sFilePath, $iLine, $sText, $bOverWrite = False, $bFill = False)
    If $bOverWrite = Default Then $bOverWrite = False
    If $bFill = Default Then $bFill = False
    If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
    If $iLine <= 0 Then Return SetError(4, 0, 0)
    If Not (IsBool($bOverWrite) Or $bOverWrite = 0 Or $bOverWrite = 1) Then Return SetError(5, 0, 0)
    If Not IsString($sText) Then
        $sText = String($sText)
        If $sText = "" Then Return SetError(6, 0, 0)
    EndIf
    If Not IsBool($bFill) Then Return SetError(7, 0, 0)
    ; Read current file into array
    Local $aArray = FileReadToArray($sFilePath)
    ; Create empty array if empty file
    If @error Then Local $aArray[0]
    Local $iUBound = UBound($aArray) - 1
    ; If Fill option set
    If $bFill Then
        ; If required resize array to allow line to be written
        If $iUBound < $iLine Then
            ReDim $aArray[$iLine]
            $iUBound = $iLine - 1
        EndIf
    Else
        If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0)
    EndIf
    ; Write specific line - array is 0-based so reduce by 1 - and either replace or insert
    $aArray[$iLine - 1] = ($bOverWrite ? $sText : $sText & @CRLF & $aArray[$iLine - 1])
    ; Concatenate array elements
    Local $sData = ""
    For $i = 0 To $iUBound
        $sData &= $aArray[$i] & @CRLF
    Next
    $sData = StringTrimRight($sData, StringLen(@CRLF)) ; Required to strip trailing EOL
    ; Write data to file
    Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE)
    If $hFileOpen = -1 Then Return SetError(3, 0, 0)
    FileWrite($hFileOpen, $sData)
    FileClose($hFileOpen)
    Return 1
EndFunc   ;==>_FileWriteToLine

So, as the latest author of the function.....

DonChunior,

Quote

the _FileWriteToLine function completely rewrites the file and does not just edit the corresponding line!

As you can see from the above code, that is exactly what it does - there is no other way to allow for the function to both insert and delete lines. So, as BrewManNH has pointed out, what you are seeing is by design and not a bug.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
×
×
  • Create New...