Jump to content

Function array question


BoogY
 Share

Recommended Posts

Hello everyone

I need to make a function that writes an array of data in to a file. But i dont know the size of the array.

i hav this function so you can see what i meen:

Func writeFile($file,$arrayData[])
    $stringFile     = FileOpen($file,1)
    
    If $stringFile = -1 Then
        MsgBox(16, "Error", "Imposible d'ouvire " & $file)
    EndIf
    
    For $line IN $arrayData
        FileWriteLine($stringFile, $line)
    Exit
    
    FileClose($file)
EndFunc

how can i make this function ?

Thanks a lot for your help

Link to comment
Share on other sites

Hello everyone

I need to make a function that writes an array of data in to a file. But i dont know the size of the array.

i hav this function so you can see what i meen:

Func writeFile($file,$arrayData[])
    $stringFile     = FileOpen($file,1)
    
    If $stringFile = -1 Then
        MsgBox(16, "Error", "Imposible d'ouvire " & $file)
    EndIf
    
    For $line IN $arrayData
        FileWriteLine($stringFile, $line)
    Exit
    
    FileClose($file)
EndFunc

how can i make this function ?

Thanks a lot for your help

Hi,

Your For loop might work as well, but change Exit to Next and put the For Loop into an else of your if condition:

If $stringFile = -1 Then
    MsgBox(16, "Error", "Imposible d'ouvire " & $file)
Else
    For $line IN $arrayData
        FileWriteLine($stringFile, $line)
    Next
EndIf

This should work as well:

Func writeFile($file,$arrayData) ; change $arraydata [] to $arraydata
    $stringFile     = FileOpen($file,1)
    
    If $stringFile = -1 Then
        MsgBox(16, "Error", "Imposible d'ouvire " & $file)
    Else    
        For $i = 0 To  UBound ($arrayData) - 1 ; depending on your array you may start with $i = 1
                FileWriteLine($stringFile, $arraydata [$i])
        Next
    EndIf
    FileClose($file)
EndFunc
Edited by 99ojo
Link to comment
Share on other sites

In documentation of AutoIt on function FileWrite the last line says this:

If the line is a binary type variant (and not text) it will be written to the file byte by byte. Binary operation can also be forced by using Fileopen with the binary flag.

I guess this is what you wanted to do. Here is a simple example of forced byte by byte writing:

Func writeFile($fileName, $arrayData)
    Local $fileHandle = FileOpen($filename, 16 + 1)
    
    If $fileHandle = -1 Then
        MsgBox(16, "Error", "Imposible d'ouvire " & $fileName)
    EndIf
    
    FileWrite($fileHandle, $arrayData)
    
    FileClose($fileHandle)
EndFunc

And I have a general suggestions for everyone: Do not use FileWriteLine Edit: and FileReadLine too.

Edited by Manadar
Link to comment
Share on other sites

Yes, as far as FileWriteLine goes: It's the same as FileWrite but with an extra check. Very often you don't want to have @CRLF characters included at the end of your string, and even if you do adding & @CRLF is not such a big deal. Choosing to use FileWrite is a more flexible and expandable solution.

FileReadLine is often called in a loop by people to read a file into an array of lines. The help file specifically says why this is bad: Use FileRead and StringSplit instead. You have more control, so it's a more flexible and expandable solution.

Only in the rare case that you want to read only one line from a file, FileReadLine is useful. FileWriteLine is only useful if you are writing a log of some kind, but then you might as well just write:

Func FileWriteLine($h, $s)
   FileWrite($h, $s & @CRLF)
EndFunc
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...