jimmyjmmy Posted April 21, 2007 Posted April 21, 2007 Hi, I want to do some counting inside the file test.txt. Each time when the script is activated, the file test.txt should increase by 1. And so I wrote the undermentioned script. However the file test.txt always read 1 no matter how many times I activate the script. What have I missed? Thanks ---------------- script ------------------ $file = FileOpen("test.txt", 2);2 = Write mode (erase previous contents) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $counter = FileReadLine($file);read file contents $counter += 1 FileWriteLine($file, $counter) MsgBox(0, "Line read:", $counter) FileClose($file)
jimmyjmmy Posted April 21, 2007 Author Posted April 21, 2007 Alright regarding the above script I found my fault and its here :-FileOpen("test.txt", 2);2 = Write mode (erase previous contents)I never knew that the contents of a file can be erased in the fileopen mode, I always thought that the contents of a file can only be changed in the write mode.Moreover I also realise that if I want to read a file then write to that same file, I have to open the file to read, close that file, then reopen that same file this time to write, then close the file.And so the script goes like this ;-------------------- script -------------------------$file1 = FileOpen("test.txt", 0);0 = Read mode; Check if file opened for reading OKIf $file1 = -1 Then MsgBox(0, "Error", "Unable to open file.") ExitEndIf$counter = FileReadLine($file1);read file contents$counter += 1FileClose($file1)$file2 = FileOpen("test.txt", 2); 2 = Write mode (erase previous contents)FileWriteLine($file2, $counter)FileClose($file2)MsgBox(0, "Counter", "this is message " & $counter)
PsaltyDS Posted April 21, 2007 Posted April 21, 2007 (edited) Cool. Looks like you fixed it. You can simplify the read portion to not bother with FileOpen(), and if the file doesn't exist start a new counter from 1: $counter = FileReadLine("test.txt", 1) If @error Then $counter = 0 $counter += 1 Since you only read once, there is no need to code the FileOpen/FileClose cycle for read. You can FileWrite that way too, but it appends to the file by default, so to overwrite line 1 the FileOpen is required. Edited April 21, 2007 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
jimmyjmmy Posted April 21, 2007 Author Posted April 21, 2007 Cool. Looks like you fixed it. You can simplify the read portion to not bother with FileOpen(), and if the file doesn't exist start a new counter from 1: $counter = FileReadLine("test.txt", 1) If @error Then $counter = 0 $counter += 1 Since you only read once, there is no need to code the FileOpen/FileClose cycle for read. You can FileWrite that way too, but it appends to the file by default, so to overwrite line 1 the FileOpen is required. Thanks a lot PsaltyDS. Its better if FileWrite has an option to overwrite file.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now