badeend Posted August 12, 2015 Share Posted August 12, 2015 Hi, I'm pretty new at autoit. I'm coding a bit to learn it. I am trying now to read an array and write it back to a file so i can verify it works. This is my code: #include <GUIConstants.au3>#include <string.au3>#include <File.au3>$path = "1"$file = FileOpen("c:\test.csv", 0)$file2 = FileOpen("c:\rekeningnrs.txt", 0)$file3 = FileOpen("c:\output.txt", 0)global $vReturn[3];_FileReadToArray ( $file2, $vReturn ); _FileWriteFromArray($file3, $vReturn); Sleep(1000);MsgBox(0,$vReturn(1)) _FileReadToArray($file2, $vReturn, $FRTA_NOCOUNT, ";") If IsArray($vReturn) Then MsgBox(0, "hoera!", "hoera!") EndIf _FileWriteFromArray("c:\output.txt", $vReturn,1); Display the file.ShellExecute("$file3") The "hoera" part is to show it works. The write from array does not generate an output. The txt file stays empty. Any thoughts? Kind regards Link to comment Share on other sites More sharing options...
Jfish Posted August 12, 2015 Share Posted August 12, 2015 Hello. It is usually better if you post your code using the code tags <> to make it more readable. I would add #include<Array.au3> at the top and then do this:_FileReadToArray($file2, $vReturn, $FRTA_NOCOUNT, ";") _arrayDisplay($vReturn)To see what is in the array you created (if anything). Also, you may want to error check along the way to see if the previous functions worked. Also, I think your IsArray might return true regardless because you initialized the variable as an array when you said $vReturn[3]. Example:global $someArray[3] if IsArray($someArray) Then MsgBox(0,"","yeah") EndIf Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
JohnOne Posted August 12, 2015 Share Posted August 12, 2015 ShellExecute($file3) Jfish 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Jfish Posted August 12, 2015 Share Posted August 12, 2015 (edited) I don't think you need to open $file3 since it is in read only mode anyway (so presumably it already exists - and read only means you can't write to it - it is the same file you are trying to write the array back to but you are using the full path instead of the handle). If the file remains open it will be blank. I would comment out that line and re-run it. Edited August 12, 2015 by Jfish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted August 12, 2015 Share Posted August 12, 2015 badeend,The two UDF's that you are using require a file path as the first parameter, not a file handle. Some rudimentary error checking goes a long way toward solving these kinds of problems. Each function has clearly defined return codes in the Help file. This is one possible way to do what I think your code was attempting...#include <File.au3> #include <array.au3> Global $vReturn[3] _FileReadToArray(@ScriptDir & "\test.csv", $vReturn, $FRTA_NOCOUNT, ";") ; 1ST parm is file path, not handle ConsoleWrite(@error & @CRLF) ; display return from function _arraydisplay($vReturn) ; display the array If IsArray($vReturn) Then MsgBox(0, "hoera!", "hoera!") EndIf _FileWriteFromArray(@scriptdir & "\output.txt", $vReturn, 0) ; array is 2d with 1 row and three columns ConsoleWrite(@error & @CRLF) ; display return from function ; Display the file. ShellExecute(@scriptdir & "\output.txt") ; file path, not handle for first parmThis is the file I used for testing test.csvkylomas Jfish and EmilyLove 2 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Jfish Posted August 12, 2015 Share Posted August 12, 2015 (edited) The two UDF's that you are using require a file path as the first parameter, not a file handle@kylomas - it looks like _FileReadToArray requires the path and _FileWriteToArray can use a path or a handle (I was not sure if that was a typo in the help file so I tested it and it worked). Strange?#include <File.au3> #include <Array.au3> $path=@scriptdir&"\testfile.txt" $path2=@scriptdir&"\testfile2.txt" $file=FileOpen($path2,2) global $testArray _FileReadToArray ($path, $testArray,$FRTA_COUNT,",") _ArrayDisplay($testArray) _FileWriteFromArray($file,$testArray) FileClose($file)Edit: and I also tried the handle with _FileReadToArray and confirmed it does not work. Edited August 12, 2015 by Jfish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted August 12, 2015 Share Posted August 12, 2015 jfish,Very nice! Generally it is wise to stay with whatever is documented in the Help file as that is what is supported, unless it is in error as may be the case here.kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Jfish Posted August 12, 2015 Share Posted August 12, 2015 jfish,Very nice! Generally it is wise to stay with whatever is documented in the Help file as that is what is supported, unless it is in error as may be the case here.kylomasJust to be clear, the help file is correct albeit inconsistent between the two functions. This is what it says for the _FileWriteFromArray:Path of the file to write to, or a file handle returned by FileOpen(). (emphasis added)I wasn't sure why they were different after I confirmed that _FileWriteFromArray does indeed work with a handle I re-read _FileReadFromArray and confirmed it does not. Which is what the help file says: Path and filename of the file to be read.It is just interesting to me that they are so closely related but one can use a handle and the other cannot. Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted August 12, 2015 Share Posted August 12, 2015 jfish,Good catch, I missed that.kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jchd Posted August 13, 2015 Share Posted August 13, 2015 The read function can and does detect file encoding hence using open before (working with a handle) would be extra work, but you need to open first to specify the encoding for writing. I'd say it's one of the least worrysome inconsistancy in the standard functions/UDFs. Jfish 1 This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Jfish Posted August 13, 2015 Share Posted August 13, 2015 @jchd I agree that it is not a worrisome difference and your comments are always interesting to me - I learn a lot from them. I think the only very small downside (if any) would be when the file did not exist and you were creating it with FileOpen (instead of opening it). In that case you would still have to use the path to the file instead of the handle on the subsequent read function. I don't think it is a big deal but that might be the only time it makes you do something extra. Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
kylomas Posted August 13, 2015 Share Posted August 13, 2015 (edited) jfish - Not sure what you are saying here... I think the only very small downside (if any) would be when the file did not exist and you were creating it with FileOpen (instead of opening it). In that case you would still have to use the path to the file instead of the handle on the subsequent read function.This...??#include <FileConstants.au3> local $sFile = @scriptdir & '\test.txt' local $hFL = fileopen($sFile,$FO_OVERWRITE) if $hFL = -1 then exit msgbox(0,'','open error') local $str = fileread($hFL) if @error then exit msgbox(0,'',@ERROR) fileclose($hfl) msgbox(0,'Of Course the file is empty','Number of bytes read = ' & @extended) kylomasedit: Incidentally, I just downloaded and read portions of the "dummies" tutorial you wrote. Outstanding job!!!! Edited August 13, 2015 by kylomas comment Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Jfish Posted August 13, 2015 Share Posted August 13, 2015 (edited) @kylomas - thanks for downloading the book. I am glad you liked it. Any and all suggestions welcome on improving it.I think we were using "read" a bit loosely. It was shorthand for "_FileReadToArray". With that context this is what I meant:#include <File.au3> $fileHandle=FileOpen(@scriptdir&"\testfile.txt",2) FileWrite($fileHandle,"1,2,3,4,5,6,7") global $testArray _FileReadToArray(@scriptdir&"\testfile.txt", $testArray,$FRTA_COUNT,",") FileClose($fileHandle)Note that in the _FileReadToArray I am unable to use $fileHandle and have to use the path again. Edited August 13, 2015 by Jfish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
badeend Posted August 13, 2015 Author Share Posted August 13, 2015 First of all, thanks for the quick response from all you guys!The writing works now. So also thanks for that. I have a new question, actually a few. 1. The @error, how can i see the values this thing gives? For example: @error:1 - File is read-only2 - Unable to open the file3 - Unable to write to the file2. consolewrite: Same question really. It writes data to the STDOUT stream. But that does not teatch me much :).3. I would like to check my test array1 for all the possible values of another array2. How would i go about doing that?I was thinking something along the lines of:Load first value of array2, search array 1, make array of results in new arrayLoad second value of array 2,..., make new column in results array with resultsand so on. Or is there a better way to do this? 4. Lastly, if i want to use a value from an array all i need to do is type:local $value= array(1) right? Because i keep on getting that the value is badly formatted (if i use []) and that the variable can't be accessed in this manner.Any tips on things to read on arrays are more than welcome. I read the wikipage allready but i think i need a bit more for what i'm doing. Kind regards! Link to comment Share on other sites More sharing options...
Jfish Posted August 13, 2015 Share Posted August 13, 2015 I am not sure where to start. I think you need to refer to the help file for a lot of these items. That said, if you want to know the value of @error you could do something like this:If @error then MsgBox(0,"Error","This is the error: "& @error) EndIfConsoleWrite can also be used to view the output with SCitE. You can see the output in the bottom pane.Arrays. I would check the help file for language reference - variables. After you know how to refer to an item in an array look at the Array UDF for more advanced operations. Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now