Jump to content
Sign in to follow this  
jimmyjmmy

How to read and write to a file

Recommended Posts

jimmyjmmy

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)

Share this post


Link to post
Share on other sites
jimmyjmmy

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 OK

If $file1 = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$counter = FileReadLine($file1);read file contents

$counter += 1

FileClose($file1)

$file2 = FileOpen("test.txt", 2); 2 = Write mode (erase previous contents)

FileWriteLine($file2, $counter)

FileClose($file2)

MsgBox(0, "Counter", "this is message " & $counter)

Share this post


Link to post
Share on other sites
PsaltyDS

Cool. Looks like you fixed it. :shocked:

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

Share this post


Link to post
Share on other sites
jimmyjmmy

Cool. Looks like you fixed it. :shocked:

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.

Share this post


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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.