Modify

Opened 5 years ago

Closed 4 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 Changed 5 years ago by Jos

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

Jos

comment:2 Changed 5 years ago by anonymous

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 Changed 5 years ago by Melba23

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 it 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

Version 0, edited 5 years ago by Melba23 (next)

comment:4 Changed 5 years ago by Melba23

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 Changed 5 years ago by anonymous

this 3 modes idea seems great imo
it should please everybody

comment:6 Changed 5 years ago by BrewManNH

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 Changed 5 years ago by anonymous

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

comment:8 Changed 5 years ago by BrewManNH

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 Changed 4 years ago by Jon

  • Component changed from AutoIt to Standard UDFs

comment:10 Changed 4 years ago by Jpm

  • Owner set to Melba23
  • Status changed from new to assigned

comment:11 Changed 4 years ago by Melba23

  • Milestone set to 3.3.15.3
  • Resolution set to Fixed
  • Status changed from assigned to closed

Fixed by revision [12304] in version: 3.3.15.3

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The owner will remain Melba23.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.