boog Posted January 25, 2008 Share Posted January 25, 2008 Hi All, I'm working on a project that involves users being able to add a line of XML into an existing XML file. The process so far opens the file using FileOpen() and then reads the first few lines and does some validation to make sure that it's the right kind of file and to determine what stylesheet to insert depending on those checks. The Stylesheet link must be placed in the second line of the document. Once I've determined that the document is good and what the stylesheet should be I write a new file using filewriteline() adding the stylesheet link in line 2. So I have 2 questions. 1) When I do the filewriteline() I'm not getting all the content of the source file in the new file despite using 'if @error= -1 then ExitLoop' in the write loop after the filewritelin() command before i close the file and return to the GUI Is there a buffer that needs to empty to write the file before I close it?? 2) Is there a better way of inserting the line in the file without having to go right the way through it and create a second file? Thanks in advance for any suggestions.. Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 I guess you have to show some snippet of code that demonstrates your issue because things should work fine and the file should contain all info after it is closed. SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 I guess you have to show some snippet of code that demonstrates your issue because things should work fine and the file should contain all info after it is closed.This is the function that deals with the files.$ofile is the output filename$xfile id the input filenameWhen the file opens in IE - it's showing that it only has 56000 or so of the total number of linesIf i use a smaller file the same thing happens.Thenks again..func savethefile($ofile,$xfile) $file=FileOpen($xfile,0) $savfile=FileOpen($ofile,2) ;reads the first line and writes it to the new file FileWriteLine($savfile,filereadline($file)) ;decides which file it is and puts the right stylesheet as the second line if $ismsg==1 Then FileWriteLine($savfile,"<?xml-stylesheet type='text/xsl' href='xml_msg_html_v0_9.xsl' version='1.0'?>") ElseIf $isib==1 Then FileWriteLine($savfile,"<?xml-stylesheet type='text/xsl' href='xml_ib_html_v0_9.xsl' version='1.0'?>") EndIf $linenumber=2 ;if the file already had a stylesheet and the user wanted it replaced we skip a line here if $removeline2==1 Then Filereadline($file) $linenumber+=1 EndIf ;then carry on reading until eof is reached. while 1 $ltw=filereadline($file) FileWriteLine($savfile,$ltw) if $ltw=="</FileDump>" then ExitLoop if @error= -1 then ExitLoop $linenumber+=1 WEnd ;cleanup - reset the input and output field names GUICtrlSetData($xmlfile,$xmlfiletext) GUICtrlSetData($outfile,$outfiletext) ;show the user it's done UpdateStatus("Processing complete") FileClose($outfile) FileClose($file) if GUICtrlRead($chk)=$GUI_CHECKED Then ShellExecute("iexplore.exe",$ofile,"") UpdateStatus("Opening Internet Explorer") EndIf Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 (edited) would make it a LOT easier (for me) if you post something I can run and test with ..... EDIT: one thing that wrong in the code is your test for EOF: $ltw = FileReadLine($file) FileWriteLine($savfile, $ltw) If $ltw == "</FileDump>" Then ExitLoop If @error = -1 Then ExitLoop $linenumber += 1 you should place the test right after the FileReadLine() since @error reports the Error of the last performed function. Edited January 25, 2008 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 Thanks for that. It was a small piece of clean up I did before posting! I've attached 3 files. convertxml.au3 and small_source.xml xml_msg_html_v0_9.xsl You'll need all 3 to see the full result. Thanks again... Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 Sorry I can't upload the XSL file .. Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 Sorry I can't upload the XSL file ..easiest is to put the lot in a zip file SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 here is the zip file.. tks Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 OK ...I have the files and had a quick look but am not sure what steps to do to reproduce your problem or what i should see.. could you explain ? I did comment the sleep(500) to make it go a bit faster reading through the input file ... SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 Great - thanks.. once you run it you select the xml file and give a name for the new file. hit create file and it will look at the xml and writ the new file. On both my PC's it truncates the output file at line 365 thanks again for looking Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 (edited) Used small_source.xml as input and created Test.xml as output and they are identical except the added second line and a different NewLine character. Input file has 0a and the new file 0d0a.. don't see it truncated at all ... Edited January 25, 2008 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 Now that's strange. I've just run it again and I'm getting the output file truncated to 365 lines. The readline command is working fine - I've just out put it's contents to the command window. If I do larger files they truncate close to the end too.. Any suggestions - Is there any reason fro the script to clash with anything else? Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 (edited) Are you running from a network drive or other type of removable drive ? What version of AutoIt3 are you running ? Edited January 25, 2008 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 Files were on separate network drives. But even if I move them to my local drive I get the same results. Link to comment Share on other sites More sharing options...
Developers Jos Posted January 25, 2008 Developers Share Posted January 25, 2008 I cannot get it to go wrong here .... ... What do you see in the SciTE outputpane when you use this while...wend loop ? while 1 $ltw=filereadline($file) if @error= -1 then ExitLoop FileWriteLine($savfile,$ltw) ConsoleWrite($linenumber & $ltw & @CRLF) ;~ sleep(500) $linenumber+=1 WEnd SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
boog Posted January 25, 2008 Author Share Posted January 25, 2008 I get all the lines in the console... that's why I think it's a filewriteline problem.. Link to comment Share on other sites More sharing options...
boog Posted January 28, 2008 Author Share Posted January 28, 2008 Hey Jos, Wanted to say a bit thank you for taking a look at this.... I had a blinding flash of light - and realised that The file handle that I was closing wasn't the right one. This was causing that last write to the file to be buffered. and me to not see it.. thanks for taking the time to look.. Boog. 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