Sign in to follow this  
Followers 0
SharpDressedMan

FileOpen read and write

8 posts in this topic

Hi all,

I'd like to open an existing file for both reading and writing. (Just like the C function fopen() does with parameter mode = "r+").

I tried FileOpen() with parameter mode = $FO_OVERWRITE (2) but this erases the file content right at opening...

Can someone explain me how to do that, if possible ?

AutoIt help for function FileSetPos() let me think that both reading and writing to a file is allowed with AutoIt, but how doing so with an existing file ?

Thanks by advance for your help.

Share this post


Link to post
Share on other sites



All in Help File !

Try:

FileOpen( "file",1+8)
$FO_READ (0) = Read mode (default)
    $FO_APPEND (1) = Write mode (append to end of file)
    $FO_OVERWRITE (2) = Write mode (erase previous contents)
    $FO_CREATEPATH (8) = Create directory structure if it doesn't exist (See Remarks).
    $FO_BINARY (16) = Force binary mode (See Remarks).
    $FO_UNICODE or $FO_UTF16_LE (32) = Use Unicode UTF16 Little Endian reading and writing mode. Reading does not override existing BOM.
    $FO_UTF16_BE (64) = Use Unicode UTF16 Big Endian reading and writing mode. Reading does not override existing BOM.
    $FO_UTF8 (128) = Use Unicode UTF8 (with BOM) reading and writing mode. Reading does not override existing BOM.
    $FO_UTF8_NOBOM (256) = Use Unicode UTF8 (without BOM) reading and writing mode.
 

a


Regards,
 

Share this post


Link to post
Share on other sites

$FO_OVERWRITE

What did you think it would do?

What other flags did you see in the help file?

 

Well, fopen(..., "r+") does allow overwriting part of the content of a file if you write to it, but does not overwrite the whole file.

That's certainly what I supposed when I tried this flag.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

All in Help File !

Try:

FileOpen( "file",1+8)
$FO_READ (0) = Read mode (default)
    $FO_APPEND (1) = Write mode (append to end of file)
    $FO_OVERWRITE (2) = Write mode (erase previous contents)
    $FO_CREATEPATH (8) = Create directory structure if it doesn't exist (See Remarks).
    $FO_BINARY (16) = Force binary mode (See Remarks).
    $FO_UNICODE or $FO_UTF16_LE (32) = Use Unicode UTF16 Little Endian reading and writing mode. Reading does not override existing BOM.
    $FO_UTF16_BE (64) = Use Unicode UTF16 Big Endian reading and writing mode. Reading does not override existing BOM.
    $FO_UTF8 (128) = Use Unicode UTF8 (with BOM) reading and writing mode. Reading does not override existing BOM.
    $FO_UTF8_NOBOM (256) = Use Unicode UTF8 (without BOM) reading and writing mode.
 

a

 

Thanks Trong but I don't need to create the directory structure; the file I want to open already exists.(All in my post ;))

Edited by SharpDressedMan

Share this post


Link to post
Share on other sites

if it doesn't exist, Else remove it (8)


Regards,
 

Share this post


Link to post
Share on other sites

Use append mode.


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

Share this post


Link to post
Share on other sites

Thanks Trong and BrewManNH,

The answer was quite obvious but "append" did not sound to be the right thing to do, to me... as I want the file position to be set at begining of the file.

What I need to do to mimic fopen("filename", "r+") is then:

$file = FileOpen("filename", $FO_APPEND)

FileSetPos($file, 0, $FILE_BEGIN)

Thanks for your answers,

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