Sign in to follow this  
Followers 0

saving an array after _ArrayPush

7 posts in this topic

Posted

I have searched but can't find anything about how to save an array after doing an _ArrayPush. I'm trying to save the array as a list with one array element to a line. This is what I've tried and it displays my array with Hello world! at the top but doesn't save it to the file.

Thanking you,

Joe

#include <Array.au3>
#include<File.au3>
Dim $echrDone
$echrdonefile = @MyDocumentsDir & '\AutoIt_code\getter\resources\ECHR_done.txt'
$file = FileOpen($echrdonefile, 1)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open " & $echrdonefile)
Exit
EndIf
FileClose($file)
If Not _FileReadToArray($echrdonefile, $echrDone) Then
MsgBox(4096, "Error", " Error reading log to Array	 error:" & @error)
Exit
EndIf
$echrDone = _ArrayUnique($echrDone)
_ArrayDisplay($echrDone, "$aRecords")
_ArrayPush($echrDone, "Hello world!", 1)
FileClose($file)
_ArrayDisplay($echrDone2, "$avArrayTarget AFTER _ArrayPush() string to beginning")
_FileWriteFromArray($file, $echrDone2, 1)

Share this post


Link to post
Share on other sites



Posted

This adds the array to the end of the existing file:

#include 
#include 
Dim $echrDone
$echrdonefile = "C:temptest.txt"
$file = FileOpen($echrdonefile, 1)
; Check if file opened for reading OK
If $file = -1 Then
MsgBox(0, "Error", "Unable to open " & $echrdonefile)
Exit
EndIf
If Not _FileReadToArray($echrdonefile, $echrDone) Then
MsgBox(4096, "Error", " Error reading log to Array error:" & @error)
Exit
EndIf
$echrDone = _ArrayUnique($echrDone)
_ArrayDisplay($echrDone, "$aRecords")
_ArrayPush($echrDone, "Hello world!", 1)
_ArrayDisplay($echrDone, "$avArrayTarget AFTER _ArrayPush() string to beginning")
_FileWriteFromArray($file, $echrDone, 1)

Share this post


Link to post
Share on other sites

Posted

Hi Joe,

first you can leave out the part with the FileOpen() and FileClose()s :-) Too much code you don't need.

Add an error check to your _FilereadToArray() and FileWriteFromArray() functions. (Get the returncode and @error macro)

Look at the helpfile about _FileWriteFromArray(). Use the @CRLF macro as delimiter.

;)

Share this post


Link to post
Share on other sites

Posted

Thanks for attempting to help me but the resulting file after _FileWriteFromArray is just the same file as the original file _FileReadToArray - I want to know how to end up with a saved a file that has the added Hello world! as the first line.

Joe

Share this post


Link to post
Share on other sites

Posted

To get the "Hello world" record in the output file you have to write the whole array and start with index = 0. Hence change

_FileWriteFromArray($file, $echrDone, 1)
to
_FileWriteFromArray($file, $echrDone, 0)

Share this post


Link to post
Share on other sites

Posted

Water,

That's the trick! Thanks for that I think I see the logic of this now.

Chrres,

Joe

Share this post


Link to post
Share on other sites

Posted

:D

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




  • Recently Browsing   0 members

    No registered users viewing this page.