Sign in to follow this  
Followers 0
Rhyono

Using FileOpen

15 posts in this topic

This looks like an extremely straightforward function...yet it isn't working for me.

I have a text file with 20 characters, no weird characters. It is only one line, and it is in the same folder as the script. So I tried to open it using $file = FileOpen("text.txt",0) and it didn't work. The file was made in notepad on W7.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Rhyono,

Have you looked at the help file for "fileopen"? $file contains a handle to your file which is now open in read mode, assuming that the file exists. You can now read the file with fileread or filereadline. Given the size of the file it would be just as easy fot you to read it like this

$file_contents = fileread(@scriptdir & "text.txt")

kylomas

edit: spelling

Edited by 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

Share this post


Link to post
Share on other sites
This contains a bad example then, since it doesn't give any indication that you need to specify the directory and implies otherwise.

Share this post


Link to post
Share on other sites

Rhyono,

There is nothing in the doc that intimates file search order. The example could be improved, however, this is the first time that I can recall anyone making the assumption that Autoit somehow finds the file.

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

Share this post


Link to post
Share on other sites

Sure we can read the example anyway we like.

'Filename' is like saying 'filepath' and a filepath is either relative or a full path.

I have found that it is good practice for me to always tell the program where to look as dialogs will often change a working directory, and that destroys the precision I require for my programs to locate files.

I'm glad there's a help at all.


I am not a lawyer.  (-_-) Xandy About  (^o^) Discord - Xandy Programmer

Share this post


Link to post
Share on other sites

By "improved" you mean it could be changed to an example that would work in at least one instance? If you saw an example which did not specify a directory, and you assumed it was written by someone who knew what they were doing, I believe you'd be more inclined to think "it must use the current directory" over "this guy is lazy."

@Xandy If the example had used a placeholder like "filename": yeah, I would've assumed path too. However, it specifically used just a filename.

1 person likes this

Share this post


Link to post
Share on other sites

Rhyono,

I will agree that the example scripts should at least run, as coded. I'll put something in the thread that handles these kinds of problems.

kylomas

1 person likes this

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

Share this post


Link to post
Share on other sites

Actually, that example should read closer to this...

Local $sPath = @ScriptDir
Local $sFile = "test.txt"
Local $hFile = FileOpen($sPath & '' & $sFile, 0)

; Check if file opened for reading OK
If $hFile = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

FileClose($hFile)

This way... it breaks it down properly to the reader:

1) The Path that the file resides in.

2) The File Name with Extension.

3) The Handle of the File, once opened.

This will kill the idea that the reader only needs a file name to work.

Just my opinion.

1 person likes this

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Rhyono,

ripdad's example looks good to me, what does it look like from a new user's perspective?

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

Share this post


Link to post
Share on other sites

This contains a bad example then, since it doesn't give any indication that you need to specify the directory and implies otherwise.

Guys, come on, where are the days we also expect people to think and understand what is happening when programming.

Whatever we do in the examples, there always will be something that could be "improved".

In this case my humble opinion is that the examples are fine and have been for the past 10 years.

Jos

2 people like this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

You cannot underestimate the stupidity of people though.

I made a snippet for a file function, and felt the need to actually create the file to be worked on

in its example by using this rule.

But I do agree, that if someone cannot figure that out, they probably have no business entering

into the world of scripting.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I was not trying to imply that the OP is stupid. ;) merely that this should be considered a learning moment and not an issue with the helpfile.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Everyone starts from somewhere. We all had to do this in the beginning. A new programmer will have to understand what a path is, before they learn anything else.

I can't remember, but I'm sure I had some difficulty understanding it at first. I wasn't born with that knowledge. It takes some time to grasp basic coding practices.

So, for someone reading the help file for the very first time (about files), it helps to have a proper structure in order to better understand what they are reading.

Anyways, just my 2 cents (which used to buy some bubble gum).

2 people like this

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites

Just for the record: I am quite capable in C++, Java, Python, Perl, BASIC, and Visual Basic. I'm extremely capable in PHP and Javascript. In those languages, when I look at the help files and I see example code and it doesn't work for me, I assume I did something wrong, rather than the example being wrong itself. I chose this language due to a few shortcomings of Java's Robot Class and transcribed a bot from a language I have had a lot of experience with to a language I've had no experience with in a matter of a couple hours. I disagree with the implication, that several of you have made, that I am inept.

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