Jump to content

FileWrite() works without FileOpen()


atomman
 Share

Recommended Posts

FileWrite() seems to work fine without using FileOpen() first.

I searched the forum and changelog, but found no info as to why. Now i'm wondering if the file is closed properly if you FileWrite() using a handle, though it seems to be.

From the help file...

Remarks

The text file must be opened in write mode or the FileWrite command will fail.

Link to comment
Share on other sites

FileWrite() seems to work fine without using FileOpen() first.

I searched the forum and changelog, but found no info as to why. Now i'm wondering if the file is closed properly if you FileWrite() using a handle, though it seems to be.

From the help file...

It always has. So do FileWriteLine() and FileRead(). However in the cases of FileWrite() and FileWriteLine(), if the file already exists they will only append text. In the situation where the file already exists you could always delete the file and then use either of the write functions to create a new one.. The reason I have never suggested that the FileOpen() help page be updated is that it would only insert one more level of confusion. Both FileWrite() and FileWriteLine() pages explain that if the file does not exist it will be created.

Clear as mud?

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

The reason I have never suggested that the FileOpen() help page be updated is that it would only insert one more level of confusion.

i think it's confusing the way it is because to a new user (me), i'm left with the crystal clear impression that i HAVE to call FileOpen() first or it will fail.

it's no big deal -- once you know that the help file is incorrect :P -- but i would think it should be changed. i'm not seeing how it would be any more confusing and, for me at least, as a new user, it's more confusing to find out that FileWrite() works in a way that the help file says it will not.

and thanks for the help!

EDIT...

OH! and since it works the way it is, are files that are written to closed properly if a file handle is used in FileWrite() (or a path for that matter)? or do i need to FileClose()?

more confusion :D

Edited by atomman
Link to comment
Share on other sites

  • Developers

i think it's confusing the way it is because to a new user (me), i'm left with the crystal clear impression that i HAVE to call FileOpen() first or it will fail.

it's no big deal -- once you know that the help file is incorrect :D -- but i would think it should be changed. i'm not seeing how it would be any more confusing and, for me at least, as a new user, it's more confusing to find out that FileWrite() works in a way that the help file says it will not.

and thanks for the help!

What part is confusing in the helpfile ?

Append a line of text to the end of a previously opened text file.

FileWrite ( filehandle or "filename", "line" )

Parameters

filehandle The handle of a file, as returned by a previous call to FileOpen. Alternatively, you may use a string filename as the first parameter.

line The line of text to write to the text file. The line is written as is - no @CR or @LF characters are added.

.. and what should be changed ?

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

i think it's confusing the way it is because to a new user (me), i'm left with the crystal clear impression that i HAVE to call FileOpen() first or it will fail.

it's no big deal -- once you know that the help file is incorrect :P -- but i would think it should be changed. i'm not seeing how it would be any more confusing and, for me at least, as a new user, it's more confusing to find out that FileWrite() works in a way that the help file says it will not.

and thanks for the help!

EDIT...

OH! and since it works the way it is, are files that are written to closed properly if a file handle is used in FileWrite() (or a path for that matter)? or do i need to FileClose()?

more confusion :D

This is the part where we can get rid of some confusion. You can't FileClose() a file That was not FileOpened(). But to throw in some more confusion (we don't want it to be too easy), If you use FileWrite(@DesktopDir & "\somefile.txt", "Foo") then there is no need to close it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

the part that i find confusing, and untrue IMO, is; "Append a line of text to the end of a previously opened text file" because the file doesn't have to have been opened previously to do a FileWrite().

also; "The handle of a file, as returned by a previous call to FileOpen" is incorrect for the same reason.

FileWrite() works with a handle or path, without ever calling FileOpen().

is that not technically incorrect, or am i not understanding something?

and should i call FileClose() after doing FileWrite() when FileOpen() is not used?

Link to comment
Share on other sites

the part that i find confusing, and untrue IMO, is; "Append a line of text to the end of a previously opened text file" because the file doesn't have to have been opened previously to do a FileWrite().

also; "The handle of a file, as returned by a previous call to FileOpen" is incorrect for the same reason.

FileWrite() works with a handle or path, without ever calling FileOpen().

is that not technically incorrect, or am i not understanding something?

and should i call FileClose() after doing FileWrite() when FileOpen() is not used?

I already stated that you can not FileClose() a file that you did not FileOpen(). BTW: just because something works does not make it the best method. I often use FileWrite() on a file that I did not FileOpen() but I've also been burned doing it. Best practice, keep this a secret and follow the help file.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

@atomman: I am sorry but your are on a "Wordsmithing' trip here. All needed information is there if you want to read it.

The only question that isn't answered is that a FiliWrite() or FileWriteLine() to a Filename will perform a Fileopen, Write the information and do a Fileclose() for you.

So when you need to write multiple lines, it is better to use a filehandle and do the Fileopen and fileclose in the script.

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

I think it makes more sense to open the file in handle form anyways. It's more efficient if you do more than one write.

oh, yeah. absolutely. i'm just focusing on the help file wording because i think it's incorrect and misleading.

if we do $handle = FileOpen(\path\) and then FileWrite($handle), then we know the file will remain open. but what about FileWrite($handle) OR FileWrite(\path\) when FileOpen is NOT used? since FileWrite() works without FileOpen(), we already know the help file is wrong since it specifically states that FileWrite() will FAIL if the file is not opened in write mode, which implies that i need to FileOpen() before i can FileWrite().

therefore, as a new user, i'm left wondering whether or not the help file is correct in telling me that it will remain open if a handle is used, or close if a path is used when FileOpen() is NOT used.

obviously this is easy to determine by testing, but i can't determine it from the help file.

Edited by atomman
Link to comment
Share on other sites

I see nothin wrong with the help file. It gives the best method to use. By that I mean the safest method. New users won't get burned if they follow the help file. Think of the confusion if the help file were to say

FileWrite() will FAIL if the file is not opened in write mode, or if "blaa blaa" or in some situations "Blaa blaa might not cause it to fail".

I still say leave the help file alone and keep this a closely guarded secret. BTW: I've pointed this out to several users in the past, it's nothing new except for those that first (they think) discover it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

yeah, i was sure this was discussed before, but i searched the forum and changelog and didn't come up with anything.

if you guys think the help is fine the way it is, no problem. i'm not going to front a case that it isn't, especially since i'm a new user and fully understand that there's a lot i DON'T understand. i was only relaying how i interpret the help file as a new user, after reading it several times.

Link to comment
Share on other sites

That should be good.

New AutoIt users are seldom confused because they use FileOpen() as the help file states (as they should as well)

Advanced users are not confused because they already know about it.

It's only those in between that get confused because they think that they have discovered something new.

Also other languages have the same issue. For example I don't think I've ever seen it pointed out that the same applies in vbs, but it does. I've often used vbs to write to a file without opening it first. Personally I would prefer to see FileOpen() removed in favor of using flags for FileReadLine(), FileWriteLine() and FileWrite() but that would open another can of worms so I'm not even going to suggest it.

BTW: Before you discover it, FileRead() does not require FileOpen() first.

You could find a lot of discrepancies in the help file if you look but you will also find those same discrepancies on MSDN.

I've been using AutoIt for a long time now and I still get confused about how people get away with certain code. Jos has a piece of code that I've never been able to figure out and I've been looking at it for a long time now.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Could also be my coding habbits :D

This is a simple line that breaks the rules as given in the help file for StringInStr(). I'll send it in a PM but I can tell you it's from the original Tidy. If it's not breaking the rules then it is at least really confusing me but that's easily done.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

OR Hobbits........whichever the habit may be. *grins*

Don't you have anything useful to do like cleaning or something? :D

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