Jump to content

_FileReadToArray


Recommended Posts

When I use the _FileReadToArray example below I get an error that says:

Error: Unknown Funtion Name.

DIM $MyArrayVariable
_FileReadToArray("C:\File.txt",$MyArrayVariable)
For $i = 1 to $MyArrayVariable[0]
$MsgBoxText = $MsgBoxText & $MyArrayVariable[$i] & @CRLF 
Next

MsgBox(0,"Array Output",$MsgBoxText)
Exit

What could be the problem here?? :D

Link to comment
Share on other sites

_FileReadToArray is a UDF (User Defined Function) as opposed to an intrinsic au3 function. This means that your script must have code that defines it.

See what the helpfile says about functions .. :D

Link to comment
Share on other sites

  • Developers

You'r missing the #include <file.au3> statement to include the UDF that contains this function...

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Or even better just copy the UDF you need in the botton of the script. So you can see it easily if you need.

I often put also a EXIT command just before Udf lines, but it is not necessary.

Disagree with the "Even better" part of your statement.

When a change is made to a standard UDF, the #include will ensure its updated everywhere. Copy&Paste means you have to go through all programs using the UDF to update manually....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

You'r missing the #include <file.au3> statement to include the UDF that contains this function...

OK, I've include the file.au3 but now I get an empty MsBox.....

(the file.txt excists, I've checked that twice)

#include <file.au3>

Dim $aArray
_FileReadToArray("C:\File.txt",$aArray)

MsgBox(0,"Array Output",$aArray)
Exit

Please help me, what am I missing.

I'm not used to working with this type of functions.

Link to comment
Share on other sites

  • Developers

You're not referring to an array in your msgbox statement....

Also its good practice to test the returncode from a function to ensure it was successful....

Have a look at this code:

#include <file.au3>

Dim $aArray
If _FileReadToArray("C:\File.txt",$aArray) then 
   MsgBox(0,"Array Output records",$aArray[0])
Else
   MsgBox(0,"Error","Error from Function")
EndIf
Exit
Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

Lets start with checking if you have the correct function in File.AU3.

Open file.au3 from the include subdir and check if the Function _FileReadToArray is the same as below and if not, replace it with this version of the function:

Func _FileReadToArray( $sFilePath, ByRef $aArray )
;==============================================
; Local Constant/Variable Declaration Section
;==============================================
Local $hFile

$hFile = FileOpen( $sFilePath, 0 )

If $hFile = -1 Then
  SetError( 1 )
  Return 0
EndIf
$aArray = StringSplit( StringStripCR( FileRead( $hFile, _
                FileGetSize( $sFilePath ) ) ), @LF )

FileClose( $hFile )

Return 1

EndFunc

This is a script that show how it should work:

#include <file.au3>     
Dim $AARRAY

; perform the _FileReadtoArray function and when the returncode = 1 show the number of records and all record contents
If _FileReadToArray("C:\file.txt", $AARRAY) Then 
   MsgBox(0, "Array Output records", $AARRAY[0])
   For $X = 1 To $AARRAY[0]
      MsgBox(0, "Record " & $X, $AARRAY[$X])
   Next
Else
  ; returncode was 0 so the function reported that the file doesn't exist
   MsgBox(0, "Error", "Error from Function")
EndIf

Exit

Have a look at both and tell me which part isn't clear to you.....

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Copied the function in the file.au3 to be sure and copied the other code in my test.au3

Now when I run the script I get the outcome = 1 so the file excists.

But the second MsBox shows no text at all while my file.txt clearly shows the line "Auto-it is the best." when I open it.

Why isn't it showing the text I entered in my file???

Link to comment
Share on other sites

Copied the function in the file.au3 to be sure and copied the other code in my test.au3

Now when I run the script I get the outcome = 1 so the file excists.

But the second MsBox shows no text at all while my file.txt clearly shows the line "Auto-it is the best." when I open it.

Why isn't it showing the text I entered in my file???

Because you can't display an array in a message box. The only valid items you can put in a message box are text, and numbers. An array is nether.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

  • Developers

:D ummhhh .. It works for me ....

Sorry for asking but: Are you sure you updated the _FileReadToArray function correctly in the correct file ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

:D ummhhh .. It works for me ....

Sorry for asking but: Are you sure you updated the _FileReadToArray function correctly in the correct file ?

Look at his code above... he is trying to display an array in the message box. It's not a problem reading the file, it's how he's displaying the result of that command.

Edit to make it eaisier to read

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

Yea, you're right. Sorry, I somehow missed that :D

Why isn't it showing the text I entered in my file???

JdeB's latest code (the one closest to the bottom as of this post) works fine for me. If you're still having errors, you might want to uninstall AutoIt and reinstall, because the intaller will insure that you have the correct version of the included UDF.

Edit: His code should display a message box with the number of lines in the file, and then 1 message box per line of the file. Works fine assuming I have the c:\file.txt textfile created.

Edited by pekster

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

  • Developers

Yea, you're right.  Sorry, I somehow missed that :)

JdeB's latest code (the one closest to the bottom as of this post) works fine for me. If you're still having errors, you might want to uninstall AutoIt and reinstall, because the intaller will insure that you have the correct version of the included UDF.

Edit: His code should display a message box with the number of lines in the file, and then 1 message box per line of the file. Works fine assuming I have the c:\file.txt textfile created.

Hé that great Pekster... you'r my personal script-explainer !!! :huh2: (Just kidding)

I thought I give the reasonal simple example and see if it would make sence....

I am typing with 2 fingers and a thumb so never type more than needed... :D

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

JdeB's latest code (the one closest to the bottom as of this post) works fine for me.  If you're still having errors, you might want to uninstall AutoIt and reinstall, because the intaller will insure that you have the correct version of the included UDF.

Could the error be created because I replaced the original Auto_it.exe with the unstable GUI-auto-it version? :D
Link to comment
Share on other sites

  • Developers

Could the error be created because I replaced the original Auto_it.exe with the unstable GUI-auto-it version?  :D

DaLiMan: Did you see my other question, because there was at some point a version of _FileReadToArray that didn't work correctly .... (i think)

To be 100% sure and just for testing you could Cut&Paste the _FileReadToArray from my earlier post into your script and remove/Comment the #include <file.au3> and see what it does then......

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

To be 100% sure and just for testing you could Cut&Paste the _FileReadToArray from my earlier post into your script and remove/Comment the #include <file.au3> and see what it does then......

I copied the function to my script and this worked fine.

And then it occured to me......I didn't give a path to the file....... :D

replaced

#include <file.au3>

With

#include <C:\file.au3>

and fixed all problems !!!

Really sometimes i just wanna...... :huh2:

Edited by DaLiMan
Link to comment
Share on other sites

  • Developers

DaLiMan,

Great !!..

BTW, you are mixing 2 #include option now..

The #include <xyz.au3> states to include the xyz.au3 from the standard include directory which is located as a subdir of the AutoIt3 program directory.

The #include "c:\xyz.au3" states to include the xyz.au3 from the specified path.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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