Zohar Posted July 5, 2012 Share Posted July 5, 2012 (edited) HiIf I open a File like this:Local $hFile =FileOpen($FilePath,0) ;0=Read $String_FileContent =FileRead($hFile) FileClose($hFile)Then I am able to successfully read the file.If I open a File like this:Local $hFile =FileOpen($FilePath,2) ;2=Write FileWrite($hFile,$String_FileContent) FileClose($hFile)Then I am able to successfully write to the file.But what If I want to do both Read+Write in 1 FileOpen+Close?How do I FileOpen() a file for both Read+Write?When I tryLocal $hFile =FileOpen($FilePath,2) ;2=Write $String_FileContent =FileRead($hFile) FileClose($hFile) Msg($String_FileContent)The messagebox shows an empty string..As If When FileOpen() with mode=2, then you can't Read from the file, only Write to it. What should I do? Edited July 5, 2012 by Zohar Link to comment Share on other sites More sharing options...
JohnQSmith Posted July 5, 2012 Share Posted July 5, 2012 (edited) Mode 2 erases the previous contents of the file so there is nothing to read.Edit: Use mode 1.From the help...1 = Write mode (append to end of file) 2 = Write mode (erase previous contents)also from the help...When reading and writing via the same file handle, the FileSetPos() function must be used to update the current file position.Edit 2: Added link to FileSetPos() Edited July 5, 2012 by JohnQSmith Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Link to comment Share on other sites More sharing options...
Zohar Posted July 5, 2012 Author Share Posted July 5, 2012 HiRegarding "1 = Write mode (append to end of file)",The help file doesn't say it can be used for both Read+Write.But I'll try it now, thank you. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 5, 2012 Moderators Share Posted July 5, 2012 Zohar,Mode 2 erases the current file contents (as explained in the Help file). However, Mode 1 will only append to the file (again as explained in the Help file). So you may need to use 2 FileOpen/Close cycles depending on what you want to do with the content - a single call might not be possible. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Zohar Posted July 5, 2012 Author Share Posted July 5, 2012 (edited) JohnQSmith:FileOpen() with mode=1 yielded the same result:FileRead() returns an Empty String..Melba23:No way to do it in 1 FileOpen() at all? :/ Edited July 5, 2012 by Zohar Link to comment Share on other sites More sharing options...
JohnQSmith Posted July 5, 2012 Share Posted July 5, 2012 (edited) Regarding "1 = Write mode (append to end of file)", Don't forget the FileSetPos() otherwise the position pointer in the file will be at the end of the file on open (hence the "append") and there will be nothing to read. Edit: So, if you wanted to read from the beginning of the file after opening it, you would need to add the following before your FileRead() command. FileSetPos( $hFile, 0, 0 ) Edited July 5, 2012 by JohnQSmith Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Link to comment Share on other sites More sharing options...
Zohar Posted July 5, 2012 Author Share Posted July 5, 2012 JohnQSmith:FileGetPos($hFile) returns 0, and not EOF..But even If I do what you recommend:FileSetPos($hFile,0,0)I still get an empty string from FileRead() Link to comment Share on other sites More sharing options...
BrewManNH Posted July 5, 2012 Share Posted July 5, 2012 Did you open the file for reading? 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 GudeHow 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 comment Share on other sites More sharing options...
JohnQSmith Posted July 5, 2012 Share Posted July 5, 2012 (edited) Umm... are you sure there's actually something in the file? If the file is empty, there is nothing to read. Edit: I just tried it, and it works fine for me (using a file with text in it)... $theFile = FileOpen("README.TXT", 1) FileSetPos($theFile,0,0) ConsoleWrite(FileRead($theFile)) FileClose($theFile) Edited July 5, 2012 by JohnQSmith Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes". Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 5, 2012 Moderators Share Posted July 5, 2012 Zohar, Try this: $hFile = FileOpen(@ScriptFullPath, 1) FileSetPos($hFile, 0, 0) $sText = FileRead($hFile) MsgBox(0, "Content", $sText) FileWrite($hFile, @CRLF & @CRLF & "New Stuff") FileSetPos($hFile, 0, 0) $sText = FileRead($hFile) MsgBox(0, "Content", $sText) FileClose($hFile) Is that what you wanted? M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Zohar Posted July 5, 2012 Author Share Posted July 5, 2012 (edited) Umm... are you sure there's actually something in the file? If the file is empty, there is nothing to read.You are right!The file's content was deleted by one of the earlier FileOpen(mode=2).Once I fixed that, FileOpen(mode=1) works!!!Thank you very much Edited July 5, 2012 by Zohar Link to comment Share on other sites More sharing options...
Zohar Posted July 5, 2012 Author Share Posted July 5, 2012 (edited) BTW,Did you notice that in the helpfile, in FileOpen(), mode=4 is missing?What is mode=4? Edited July 5, 2012 by Zohar Link to comment Share on other sites More sharing options...
BrewManNH Posted July 5, 2012 Share Posted July 5, 2012 I don't believe there is a mode 4. 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 GudeHow 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 comment Share on other sites More sharing options...
GEOSoft Posted July 5, 2012 Share Posted July 5, 2012 (edited) Mode to open the file in.Can be a combination of the following: 0 = Read mode (default) 1 = Write mode (append to end of file) 2 = Write mode (erase previous contents) 8 = Create directory structure if it doesn't exist (See Remarks). 16 = Force binary mode (See Remarks). 32 = Use Unicode UTF16 Little Endian reading and writing mode. Reading does not override existing BOM. 64 = Use Unicode UTF16 Big Endian reading and writing mode. Reading does not override existing BOM. 128 = Use Unicode UTF8 (with BOM) reading and writing mode. Reading does not override existing BOM. 256 = Use Unicode UTF8 (without BOM) reading and writing mode. 16384 = When opening for reading and no BOM is present, use full file UTF8 detection. If this is not used then only the initial part of the file is checked for UTF8.The folder path must already exist (except using mode '8' - See Remarks).Do you see anything in there that adds up to 4?That having been said; the text I just quoted needs looking at.Can be a combination of the following: is technically incorrect. As far as I am aware you can not combine 0, 1, 2.It should probably read something like0, 1 or 2 can be combined with the following: but that could be considered as nit picking.EDIT:BTW: You don't need to use FileOpen/FileClose at all for a simple FileRead() operation. Just FileRead() it to a variable, work on the variable and then write it back using FileOpen()/FileWrite()/FileClose() so you can set the write mode. Edited July 5, 2012 by GEOSoft 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!" Link to comment Share on other sites More sharing options...
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