Jump to content

FileOpen / FileWrite Bug?


 Share

Recommended Posts

This doesn't work, it deletes (overwrites) file contents:

$hFile = FileOpen("file.txt", 2)
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileWrite($hFile, $content2)
FileClose($hFile)

Why? Instead, I have to use this workaround:

$hFile = FileOpen("file.txt")
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileClose($hFile)
$hFile = FileOpen("file.txt",2)
FileWrite($hFile, $content2)
FileClose($hFile)

That's it, opening, closing and reopening the file.

Function reference says: FileWrite. "Write text/data to the end of a previously opened file." (Does it mean "append"?).

Remarks: "The file must be opened in write mode or the FileWrite() command will fail."

But what write mode? $FO_APPEND (1) = Write mode (append to end of file). $FO_OVERWRITE (2) = Write mode (erase previous contents).

 

Link to comment
Share on other sites

In the general case, changing part of a file implies reading it, performing the change then rewriting it in full.

Yet you can somehow streamline the code:

$changed = StringReplace(FileRead("file.txt"), "old", "new")
$hFile = FileOpen("file.txt", 2)
FileWrite($hFile, $changed)
FileClose($hFile)

 

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 here
RegExp tutorial: enough to get started
PCRE 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

OK, finally I discovered that AutoIt doesn't have a read&write mode, like other programming languages, but there's a workaround using FileSetPos() before each Read/Write operation.

$hFile = FileOpen("file.txt", 1)
FileSetPos($hFile, 0, 0)
$content1 = FileRead($hFile)
$content2 = StringReplace($content1, "old", "new")
FileSetPos($hFile, 0, 0)
FileWrite($hFile, $content2)
FileClose($hFile)

Reference:

FileOpen("filename", $FO_OVERWRITE) deletes file contents, at the same moment.

Link to comment
Share on other sites

The simplest way, in AutoIt, to do what you are doing is this

#include <FileConstants.au3>

$content1 = FileRead("file.txt")
$hFile = FileOpen("file.txt", $FO_OVERWRITE)
$content2 = StringReplace($content1, "old", "new")
FileWrite($hFile, $content2)
FileClose($hFile)

 

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

Isn't that what I posted above?

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 here
RegExp tutorial: enough to get started
PCRE 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

1 minute ago, jchd said:

Isn't that what I posted above?

Yes it is but oasis375 didn't appear to understood your your first compounded line, so I set it out one statement per line to make it clearer.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Link to comment
Share on other sites

8 hours ago, jchd said:

Isn't that what I posted above?

Sorry if I upset you. What I didn't understand was that AutoIt doesn't have a method to read and write at the same time, and that $FO_OVERWRITE deletes file contents, at the same moment. So mine was a conceptual problem, not a coding one.

Anyway, thank you guys for your replies.

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

×
×
  • Create New...