Sign in to follow this  
Followers 0
boog

Adding a line to a file

17 posts in this topic

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

Share this post


Link to post
Share on other sites



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.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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 filename

When the file opens in IE - it's showing that it only has 56000 or so of the total number of lines

If 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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

would make it a LOT easier (for me) if you post something I can run and test with ..... :D

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 by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Sorry I can't upload the XSL file ..

Share this post


Link to post
Share on other sites

here is the zip file.. tks :D

Share this post


Link to post
Share on other sites

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


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 ... :D

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

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? :D

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Are you running from a network drive or other type of removable drive ?

What version of AutoIt3 are you running ?

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Files were on separate network drives.

But even if I move them to my local drive I get the same results.

Share this post


Link to post
Share on other sites

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I get all the lines in the console...

that's why I think it's a filewriteline problem.. :D

Share this post


Link to post
Share on other sites

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.

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