Jump to content

Recommended Posts

Posted

FileOpen() with the right parameter?

Yes, i have done fileopen, fileread and get the value.

The value is Trade in one row. I want to replace it with NoTrade on one row, meaning

doing a rewrite with new value.

:idea:

Posted

I think what Jos meant was this:

help on FileOpen():

Parameters:

...

[mode]: [optional] Mode to open the file in

...

2 = Write mode (erase previous contents)

Posted (edited)

I think what Jos meant was this:

help on FileOpen():

Parameters:

...

[mode]: [optional] Mode to open the file in

...

2 = Write mode (erase previous contents)

So if i first do a fileopen 0 and fileread, can i depending on the result doing

a fileopen 2 which emptying the file, and then fileopen 1, filewrite with new data?

:idea:

Edited by jorgeng
Posted

So if i first do a fileopen 0 and fileread, can i depending on the result doing

a fileopen 2 which emptying the file, and then fileopen 1, filewrite with new data?

:idea:

almost..

1. FileOpen with mode=0

2. FileRead

3. FileClose

4. FileOpen with mode=2

5. FileWrite

6. FileClose

No need to fileopen with mode= 1 to write the data as mode=2 is alreay write mode. And always remember to close your file handles.

Posted

almost..

1. FileOpen with mode=0

2. FileRead

3. FileClose

4. FileOpen with mode=2

5. FileWrite

6. FileClose

No need to fileopen with mode= 1 to write the data as mode=2 is alreay write mode. And always remember to close your file handles.

Better yet, You don't need to open a file for reading

$sStr = FileRead("somefile")

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

Posted

I like to always FileOpen, FileClose. Otherwite I'm sure I'll start forgetting to properly close open files

I'm just pointing out that it doesn't have to be opened in any mode to read it. If he wants to read from the file the FileRead() does that without opening it at all. If he wants to empty the file contents then it should be

$hFile = FileOpen("somefile.txt", 2);; handles are best here

FileWrite($hFile, "")

FileClose($hFile)

And to replace the contents

$hFile = FileOpen("somefile.txt", 2);; handles are best here

FileWrite($hFile, "Some new string")

FileClose($hFile)

Remember, a file can be open for reading and writing at the same time.

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

Posted

Remember, a file can be open for reading and writing at the same time.

How would that be done? Nested fileopens don't work for me and mode 0+1 just results in write mode (obviously).
Posted (edited)

Hi,

you can read in a file, which is open in write mode, can't you?

But you can't write to file, which is open in read mode....

;-))

Stefan

Edited by 99ojo
Posted

I've tried allot of combinations, but I can't seem to do it. There is no need for me to open a file in read/write mode anyways.

I think the OP's question is answered and I'm just streching this topic now though. :idea:

Posted (edited)

Hi,

Sorry correction:

You have to use FileOpen (..., 0) to use FileRead or FileReadLine with an file handle.

See helpfile:

Failure: Sets @error to 1 if file not opened in read mode or other error.

Feeling like..... :idea:

;-))

Stefan

BTW @JohnOne: You are using a filehandle without a FileOpen -> $file isn't declared

Edited by 99ojo
  • 2 weeks later...
Posted

I would use

#Include <File.au3>

$var = Fileread($file)
_FileWriteToLine($file,$line,$text,1) ;write line overwriting the existing line

I tried this and it works, and does what i want.

I have contracted a company building a php script on server and i had problem whith the delete

file which crashed the script since php wanted access to file too. I think this is a great way

to update a single line in a file without needing to open file, check status and so on since

in my case this can happens on the same second from many scripts.

Thanks.

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