Modify

Opened 7 years ago

Closed 6 years ago

#3678 closed Bug (Fixed)

_FileWriteToLine

Reported by: anonymous Owned by: Melba23
Milestone: 3.3.15.3 Component: Standard UDFs
Version: 3.3.15.0 Severity: None
Keywords: Cc:

Description

_FileWriteToLine($file, $line, "", 1)
this was removing a line now it just delete it leaving empty line

Attachments (0)

Change History (11)

comment:1 by Jos, 7 years ago

Could you try to explain what you mean exactly, as that sentence is not clear to me?

Jos

comment:2 by anonymous, 7 years ago

ok, with a file with those lines :
1
2
3
4

doing this _FileWriteToLine($file, 3, "", 1) was result in
1
2
4

but now the result is
1
2

4

comment:3 by Melba23, 7 years ago

I see the problem - and I remember why it happens.

There was considerable discussion when the function was rewritten some years ago as to whether this "delete a line" functionality should be retained as it removes the possibility of replacing an existing line with a blank one. If it were retained, the only option to "overwrite" with a blank line was to remove the line completely and then rewrite a new blank one, which required 2 calls to the function. The consensus as I recall was that overwriting a line with a blank was more likely than deleting a line - and so the code was amended, but not the Help file.

So the question resurfaces - which of the 2 functionalities is more useful? Or should both be available? I will take a look and see what might be done to do this without overcomplicating either the syntax or the code.

M23

Last edited 7 years ago by Melba23 (previous) (diff)

comment:4 by Melba23, 7 years ago

Not too difficult - but it means using 3 modes instead of the existing 2:

#include <File.au3>
#include <MsgBoxConstants.au3>

; These constants added to FileConstants.au3
Const $FWTL_INSERT = 0
Const $FWTL_OVERWRITE = 1
Const $FWTL_DELETE = 2

$sFile = @ScriptDir & "\test.txt"
$sContent = "1" & @CRLF & "2" & @CRLF & "3" & @CRLF & "4"

FileDelete($sFile)
FileWrite($sFile, $sContent)
; Insert a line
$iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_INSERT)
ConsoleWrite($iRet & " - " & @error & @CRLF)

FileDelete($sFile)
FileWrite($sFile, $sContent)
; Overwrite a line
$iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_OVERWRITE)
ConsoleWrite($iRet & " - " & @error & @CRLF)

FileDelete($sFile)
FileWrite($sFile, $sContent)
; Delete a line - obviously the $sText parameter is ignored
$iRet = _FileWriteToLine_Mod($sFile, 3, "fred", $FWTL_DELETE)
ConsoleWrite($iRet & " - " & @error & @CRLF)

FileDelete($sFile)

; Use 3 mode options - insert, overwrite, delete
Func _FileWriteToLine_Mod($sFilePath, $iLine, $sText, $iMode = $FWTL_INSERT, $bFill = False)
	If $iMode = Default Then $iMode = $FWTL_INSERT
	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 $iMode < 0 Or $iMode > 2 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

	Switch $iMode
		Case 0 ; Insert
			$aArray[$iLine - 1] = $sText & @CRLF & $aArray[$iLine - 1]
		Case 1 ; Overwrite
			$aArray[$iLine - 1] = $sText
		Case 2 ; Delete
			_ArrayDelete($aArray, $iLine - 1)
			$iUBound -= 1
	EndSwitch

	; 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

	; Just for illustration purposes
	MsgBox($MB_SYSTEMMODAL, $iMode, $sData)

	; 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

Comments?

M23

comment:5 by anonymous, 7 years ago

this 3 modes idea seems great imo
it should please everybody

comment:6 by BrewManNH, 7 years ago

this 3 modes idea seems great imo
it should please everybody

I disagree, a function with the word Write in it shouldn't be deleting lines. If you think writing means deleting then I can't help with that.

comment:7 by anonymous, 7 years ago

Then do a LineDelete function i don't mind. There aren't any easy way to delete a line atm so...

comment:8 by BrewManNH, 7 years ago

It's very easy to delete a line, just read everything into an array, delete the element of the array containing the line you don't want, write the array back to the file.

comment:9 by Jon, 6 years ago

Component: AutoItStandard UDFs

comment:10 by J-Paul Mesnage, 6 years ago

Owner: set to Melba23
Status: newassigned

comment:11 by Melba23, 6 years ago

Milestone: 3.3.15.3
Resolution: Fixed
Status: assignedclosed

Fixed by revision [12304] in version: 3.3.15.3

Modify Ticket

Action
as closed The owner will remain Melba23.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.