Jump to content

My problem with _FileWriteToLine


Recommended Posts

Case $Combo4; Stany
            $ReadCombo = GUICtrlRead($Combo4)
            if $ReadCombo = "California" then
            $ReadFile = FileRead(@ScriptDir&'\Dzielnice\California.txt')
            GUICtrlSetData($Combo5,$ReadFile)
            _FileWriteToLine(@ScriptDir&'\cfg\ostatniedane.txt',1,'5')

I want overwrite the First line of file ostatniedane.txt but it's only adds another one :/ Anyone Could help me?

Link to comment
Share on other sites

  • Moderators

galan2015,

Re-read the help file for _FileWriteToLine and pay particular attention to the $bOverWrite parameter.

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

 

Link to comment
Share on other sites

 

$Combo5 = GUICtrlCreateCombo("http://craigslist.org|http://reddit.com|http://google.com|http://gmail.com", 184, 98, 145, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))

Func ZapisywanieMiejscaProgramu()
            $1ReadCombo = GUICtrlRead($Combo5)
        If $1ReadCombo = 'http://craigslist.org' then
            _FileWriteToLine(@ScriptDir&'\cfg\ostatniedane.txt',2,'1',1)
            Msgbox(1,1,$1ReadCombo)
            ElseIf $combo5 = 'http://reddit.com' then
            _FileWriteToLine(@ScriptDir&'\cfg\ostatniedane.txt',2,'2',1)
            Msgbox(1,1,$Combo5)
            ElseIf $1ReadCombo = 'http://google.com' then
            _FileWriteToLine(@ScriptDir&'\cfg\ostatniedane.txt',2,'3',1)
EndIf
EndFunc

 

_FileWriteToLine works only for first example.

Edited by galan2015
Link to comment
Share on other sites

What error if any is returned by _FileWriteToLine when you use it and it doesn't work?

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

Link to comment
Share on other sites

  • Moderators

galan2015,

You have discovered a bug in _FileWriteToLine - when it was rewritten the check for an error in the $bOverWrite parameter was badly formatted and so it fails regardless of the setting. I will fix it for the next release but you can use this modified function in the meantime:

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

$sFilePath = @ScriptDir & '\ostatniedane.txt'

; Simulate creation of the file
FileDelete($sFilePath)
FileWrite($sFilePath, "Fixed" & @CRLF & "Replaceable")
$sCheck = FileRead($sFilePath)
MsgBox($MB_SYSTEMMODAL, "Content", $sCheck)

$hGUI = GUICreate("Test", 500, 500)
$Combo5 = GUICtrlCreateCombo("", 184, 98, 145, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
GUICtrlSetData($Combo5, "http://craigslist.org|http://reddit.com|http://google.com|http://gmail.com")

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Combo5
            ZapisywanieMiejscaProgramu()
    EndSwitch



WEnd



Func ZapisywanieMiejscaProgramu()
    Switch GUICtrlRead($Combo5)
        Case 'http://craigslist.org'
            _FileWriteToLine_Mod($sFilePath, 2, '1', True)
        Case 'http://reddit.com'
            _FileWriteToLine_Mod($sFilePath, 2, '2', True)
        Case 'http://google.com'
            _FileWriteToLine_Mod($sFilePath, 2, '3', True)
    EndSwitch



    $sCheck = FileRead($sFilePath)
    MsgBox($MB_SYSTEMMODAL, "Content", $sCheck)

EndFunc   ;==>ZapisywanieMiejscaProgramu



Func _FileWriteToLine_Mod($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

That works for me.

M23

Edit: Fixed.

Edited by Melba23

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

 

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

×
×
  • Create New...