Jump to content

_FileWriteToLine Won't Insert


Recommended Posts

#include <File.au3>

_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite])

Parameters

$sFile The file to write to

$iLine The line number to write to

$sText The text to write

$fOverWrite if set to 1 will overwrite the old line

if set to 0 will not overwrite

Return Value

Success: 1

Failure: 0

@Error: 0 = No error

1 = File has less lines than $iLine

2 = File does not exist

3 = Error when opening file

4 = $iLine is invalid

5 = $fOverWrite is invalid

6 = $sText is invalid

Remarks

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

Example

#include <File.au3>

;Example: Write to line 3 of c:\test.txt REPLACING line 3

_FileWriteToLine("c:\test.txt", 3, "my replacement for line 3", 1)

;Example: Write to line 3 of c:\test.txt NOT REPLACING line 3

_FileWriteToLine("c:\test.txt", 3, "my insertion", 0)

============

So there's the entry from help. Note the optional $fOverWrite flag.

I set this flag to 0, or leave it blank and it SHOULD insert a line of text into the designated file... but this is not the case! Nothing happens at all.

Is there a later version of _FileWriteToLine in a later beta, or is there something someone may have figured out? Thanks in advance.

Link to comment
Share on other sites

  • Moderators

It works, but you haven't provided a .txt file you're trying to use it on, or how you are trying to use it. You should always try to keep up to date with the beta's, I think we are in .126 now.

If you want/need help, post what "you've" done, not what the help file says, we've all pretty much already read it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If you want/need help, post what "you've" done, not what the help file says, we've all pretty much already read it.

Fair enough... I cut out a bunch of unimportant stuff

Func _TM_ActionEdit(); Takes the results of the edit (in $VregD, and replaces the values in the XML file)
; ...

    $AFN = "" & $ActionPath & $TeplFilename & ".xml"
    $existingFile = FileExists($AFN)
    $ActFile = FileOpen($AFN, 0)
    If $ActFile = -1 or not $existingFile Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    
; === make sure we're at the right line ===
    $curLine = _TM_ActionFind($myname); returns the line number of $myname in the default file
    if not $curLine then 
        ConsoleWrite("Can't find "&$myname&@CR)
        return 0
    EndIf
    
; === clear out the old action entry ===
        _FileWriteToLine($AFN, $curLine, "", 1); kills the first line  === THIS WORKS FINE ===
    while not StringInStr(FileReadLine($ActFile, $curLine), "<action>") and not StringInStr(FileReadLine($ActFile, $curLine), "xml>")
    ;ConsoleWrite("Killing line :: "&FileReadLine($ActFile, $curLine)&@CR)
        _FileWriteToLine($AFN, $curLine, "", 1); === THIS WORKS FINE ===
    WEnd
    FileClose($ActFile)
    
; === insert the new action BACKWARDS! MWAHAHAHAH ===
; reminder: $VRegD[0-6] is the name, test, etc. 
;          $actionValues[end] = "i|d" where i=#inputs and d=#data. 
;          $actionValues[6>x>end] has the input & data as "i|<values>" and "d|<values>"
; now, let's populate this.
    
    $i = UBound($VRegD,2)-1; should be 8

    _FileWriteToLine($AFN, $curLine, "</action>", 0); === THIS FAILS (notice I tried '0' explicitly) ===
    ConsoleWrite("File Write Error Number "&@error&@CR)
    
    $DataCount = _TM_CtrlArrCountChildren($VRegD[1][8])
    ConsoleWrite("Data Kids = "&$DataCount&@CR)
    If $DataCount > 0 Then
        _FileWriteToLine($AFN, $curLine, " <actionData>"); === THIS FAILS (notice I left as default, should be 0) ===
        For $i = $DataCount to 1 step -1
            $LVI_ID = _TM_CtrlArrGetChild($VRegD[1][8], $i)
            $LVIvalues = GUICtrlRead($LVI_ID)
            ConsoleWrite("#"&$i&"("&$LVI_ID&") is "&$LVIvalues&@CR)
            _FileWriteToLine($AFN, $curLine, "  <data>" & $LVIvalues & "</data>"); === THIS FAILS ===
        Next
        _FileWriteToLine($AFN, $curLine, " </actionData>", 0); === THIS FAILS ===
    Else
        _FileWriteToLine($AFN, $curLine, " <actionData></actionData>"); === THIS FAILS ===
    EndIf
    
    $InputCount = _TM_CtrlArrCountChildren($VRegD[1][7])
    ConsoleWrite("Input Kids = "&$InputCount&@CR)
    If $InputCount > 0 Then
        _FileWriteToLine($AFN, $curLine, " <actionInput>", 0); === THIS FAILS ===
        For $i = $InputCount to 1 step -1
            $LVI_ID = _TM_CtrlArrGetChild($VRegD[1][7], $i)
            $LVIvalues = GUICtrlRead($LVI_ID)
        ;ConsoleWrite("#1 is "&$LVIvalues&@CR)
            _FileWriteToLine($AFN, $curLine, "  <input>" & $LVIvalues & "</input>"); === THIS FAILS ===
        Next
        _FileWriteToLine($AFN, $curLine, " </actionInput>"); === THIS FAILS ===
    Else
        _FileWriteToLine($AFN, $curLine, " <actionInput></actionInput>"); === THIS FAILS ===
    EndIf

    for $i = 6 to 0 step -1
    ;ConsoleWrite($i&"->"&GUICtrlRead($VRegD[1][$i])&@CR)
        _FileWriteToLine($AFN, $curLine, " <"&$VRegD[0][$i]&">"&GUICtrlRead($VRegD[1][$i])&"</"&$VRegD[0][$i]&">"); === THIS FAILS ===
    next
    
    _FileWriteToLine($AFN, $curLine, "<action>", 0); === THIS FAILS ===
    GUISetState(@SW_SHOW, "Guest-Tek TestMaster")
    _TM_CLOSEClicked()
    return 0
EndFunc

- Now if I explicitly set any flag to 1, it works fine. But if I set the flag to 0 or leave it blank, it doesn't do anything at all.

- The fact that an enabled overwrite flag allows for file writing means all other info is valid (file, line, text).

- I tried adding @error reader, but it's always 0 (no error) even when the _FileWriteToLine does nothing.

- I tried opening the file for reading and not opening the file... same result (open file not needed)

Link to comment
Share on other sites

  • Moderators

First thing that stands out to me, is you open the file FileOpen($AFN, 0) in Read mode. With _FileWriteToLine() you don't need to open the file first. It will do it for you.

Edit:

I relooked at your script (I didn't notice you had posted where it was failing) and tested myself, I'm using Beta .124 and .126 and they work fine 0 and 1.

Actually, on the test file all of your examples worked fine:

#include <file.au3>
_FileWriteToLine("c:\test.txt", 1, "<actionData>", 0)
_FileWriteToLine("c:\test.txt", 1, "<actionData>")
_FileWriteToLine("c:\test.txt", 3, "<actionData>", 1)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

First thing that stands out to me, is you open the file FileOpen($AFN, 0) in Read mode. With _FileWriteToLine() you don't need to open the file first. It will do it for you.

Edit:

I relooked at your script (I didn't notice you had posted where it was failing) and tested myself, I'm using Beta .124 and .126 and they work fine 0 and 1.

Actually, on the test file all of your examples worked fine:

#include <file.au3>
_FileWriteToLine("c:\test.txt", 1, "<actionData>", 0)
_FileWriteToLine("c:\test.txt", 1, "<actionData>")
_FileWriteToLine("c:\test.txt", 3, "<actionData>", 1)
That tells me exactly what I wanted to hear.

Now I know that upgrading my beta will probably fix this.

Thanks! :D

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