Jump to content

Can't Close File After Opening


Recommended Posts

Basically, I'm opening a file in mode 2, clearing it, then closing it. There is a while loop that reads from the file every few seconds, and if it contains a certain text then the file is opened and cleared again.

The problem is after the function is called to clear the file, then close it, I can't write to the file again. I get the error that the file path is invalid, which means the file is still in use.

My code is a bit messy and I put some unecessary stuff in there to try to help me catch the problem but that isn't working.

Here is the script:

; Begin script

; Set file
$filename = "teleportreader.txt"
; $text = FileRead($filename)
; Set replace
$replace = "next|special|\s"
;$text = " "
;$cleartext = StringRegExpReplace($replace,$replace,"")

while 1

$what = FileRead($filename)
If $what = "next" Then
clear()
EndIf
ToolTip("going to sleep", 0, 0)
Sleep(10000)
ToolTip("waking up", 0, 0)
Sleep(3000)
WEnd
; Clear the file
;$hf = FileOpen($filename,1)
;FileWrite($hf,$text)
;FileClose($filename)

Func clear()
  $cleartext = StringRegExpReplace($what,$replace,"")
;$hf = FileOpen($filename,2)
FileOpen($filename, 2)
FileWrite($filename,"")
FileClose("teleportreader.txt")
$fname = FileOpen($filename, 2)
FileClose($fname)
EndFunc
Link to post
Share on other sites

From the helpfile reference - FileWrite:

Do not mix filehandles and filenames, i.e., don't FileOpen a file and then use a filename in this function. Either use filehandles or filenames in your routines, not both.

I haven't tested the script but from a quick glance it looks like this could be causing your problems (possibly).

Edited by czardas
Link to post
Share on other sites

Yes you cant use FileClose with a filename.

When you work with filenames on FileRead/Write operations, the file is opened and closed automatically after the operation has finished.

Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite
Link to post
Share on other sites

Do this:

Func clear()
 $cleartext = StringRegExpReplace($what, $replace, "")
 $fname = FileOpen($filename, 2)
 FileClose($fname)
EndFunc   ;==>clear

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...