RAMMRODD Posted May 10, 2007 Share Posted May 10, 2007 Ok no flaming of the noob but I was messing around with the help file and was learning about fileread fileopen filewrite filecreate so I thought how would I open 1 file read its first line, create a new file and write file 1's first line into file 2's. This is just copying and pasting the example code so please dont point that out to me lol. What do I need to do to make this work? #include <File.au3> If Not _FileCreate("file1.txt") Then MsgBox(4096,"Error", " Error Creating/Resetting log. error:" & @error) EndIf $file = FileOpen("file1.txt", 1) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $file = FileOpen("file2.txt", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached While 1 $chars = FileRead($file, 1) If @error = -1 Then ExitLoop FileWrite($file, "$chars" & @CRLF) FileClose($file) Wend ok so I messed with it a little, its not exactly copied the exact way but please shed some light on this. are there any tutorials out for AI3? Link to comment Share on other sites More sharing options...
enaiman Posted May 10, 2007 Share Posted May 10, 2007 It would be better to use also File.au3 found into Autoit\include folder to create the file and FileReadLine, FileWriteLine to read/write. here is an example: #include <File.au3> _FileCreate(@ScriptDir&"\result.txt") ;create the file result.txt $to_read = FileOpen(@ScriptDir&"\to_read.txt", 0) ;open the file to read from in read-only mode $to_write = FileOpen(@ScriptDir&"\result.txt", 1) ;open the file to write in read-write mode FileWriteLine ($to_write, FileReadLine($to_read, 1)) ;write the line read from to_read file FileClose ($to_read) ;close files FileClose ($to_write) If you already have the file to write to, then you can get rid of include<file.au3> and _FileCreate SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
herewasplato Posted May 10, 2007 Share Posted May 10, 2007 ...If you already have the file to write to, then you can get rid of include<file.au3> and _FileCreateFrom the help file under FileOpen:When opening a file in write mode, the file will be created if it does not exist.Just FYI [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
herewasplato Posted May 10, 2007 Share Posted May 10, 2007 ...ok so I messed with it a little, its not exactly copied the exact way but please shed some light on this.... When you copied and pasted the FileOpen code, you failed to change the varable named $file to something unique. If you still want to play with this code, try this:$file1 = FileOpen("input.txt", 0) $file2 = FileOpen("output.txt", 1) ; Read in 1 character at a time until the EOF is reached While 1 $chars = FileRead($file1, 1) If @error = -1 Then ExitLoop FileWrite($file2, "$chars" & @CRLF) WEnd FileClose($file1) FileClose($file2)You can add the error checking back in after you understand the basic code. ...are there any tutorials out for AI3?See here: http://www.autoitscript.com/forum/index.ph...st&p=145357 [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
enaiman Posted May 10, 2007 Share Posted May 10, 2007 Just FYIThanks, it is good to know it.I prefered to use the _FileCreate function just to be sure that the file is created SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
xcal Posted May 10, 2007 Share Posted May 10, 2007 Thanks, it is good to know it.I prefered to use the _FileCreate function just to be sure that the file is created How do you think _FileCreate creates the file? How To Ask Questions The Smart Way Link to comment Share on other sites More sharing options...
enaiman Posted May 11, 2007 Share Posted May 11, 2007 How do you think _FileCreate creates the file?Dunno, but I'm 100% sure you're right to ask me that. I use to take a look inside UDFs and try to figure how a function is created (if I'm interrested in that function) and last time I did that for ReplaceStringInFile. I guess I took _FileCreate for granted and I didn't bothered myself to look at the code. I will take a look at it a.s.a.pAnyway I've learned something out of this discussion and I am grateful. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
herewasplato Posted May 11, 2007 Share Posted May 11, 2007 (edited) Thanks, it is good to know it.I prefered to use the _FileCreate function just to be sure that the file is created When you use #include <File.au3> you add 697 lines to your script. That is okay if you are going to use several UDFs from the included file - or - if you are going to use the nice error checking that the UDFs have built in. If you like _FileCreate, then you might want to check for any errors that it returns. If you are not going to use the error checking, then you might as well use the one line from that UDF that actually creates the file: $hOpenFile = FileOpen($sFilePath, 2)Just a thought - I do not use error checking as often as I should. :-)Edit: not picking on you - it is just that a lot of people do not know that all of the lines of an included file will be the script... even including all of those lines is not a big deal - as long as you know. Edited May 11, 2007 by herewasplato [size="1"][font="Arial"].[u].[/u][/font][/size] Link to comment Share on other sites More sharing options...
PsaltyDS Posted May 11, 2007 Share Posted May 11, 2007 Edit: not picking on you - it is just that a lot of people do not know that all of the lines of an included file will be the script... even including all of those lines is not a big deal - as long as you know.As an experiment, you can take a really simple script, like a hello world GUI with just an exit button, put #include <guiconstants.au3> at the top and compile it.Then do a decompile and look at the resulting script. It's very instructional to see what's really in there. 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 Link to comment Share on other sites More sharing options...
enaiman Posted May 13, 2007 Share Posted May 13, 2007 It is pretty obvious what is included in your file when you use "#include ..." a simple look at the files in the AutoIt\Include is enough; so far it doesn't bother me to include anything I need in order to make the script work. But later ... who knows .. SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) 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