Sign in to follow this  
Followers 0
BoogY

Function array question

6 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Thanks a lot for your help. i'l give it a try :)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

Hi Manadar,

nice too know:

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

Can you please explain your suggestion.

;-))

Stefan

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0