Jump to content

Using FileOpen


Rhyono
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

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

Link to comment
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.

Link to comment
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.

Link to comment
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.

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

Link to comment
Share on other sites

  • Developers

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

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

Link to comment
Share on other sites

  • Developers

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

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

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

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

Link to comment
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.

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