I am attempting to do the following: use the XML::Parser to extract specific information into a text file and then by selective regwx, place the extracted information into another formatted text document. Where I am having an issue is reading the first text file after it has been created and closed. For some reason I cannot read from the reopened INTFILEin or write to FMTFILE within the "while" loop (relevant code below). I did try the same steps not using the XML parser (using a print to INTFILEout of character strings) and had no problem writing to the file.

# open intermediate file for pass of XML file open (INTFILEout, '>', $intfile) or die ("Cannot open file $intfile") unless -f $intfile; # parse thru XML file into intermediate file my $parser = new XML::Parser; $parser->setHandlers( Start => \&startElement, End => \&endElement, Char => \&characterData, Default => \&default); $parser->parsefile($xmlfile); # close intermediate file from first pass of XML file close INTFILEout; # reopen intermediate file for load into formatted output file open (INTFILEin, '<', $intfile) or die ("Cannot open file $intfile") unless -f $intfile; open (FMTFILE, '>', $frmtfile) or die ("Cannot open file $frmtfile") unless -f $frmtfile; # processing intermediate file into formatted file while ($linein = <INTFILEin>){ chomp $linein; print FMTFILE "$linein\n"; } # close files used to create formatted file close INTFILEin; close FMTFILE;

Any assistance or guidance is appreciated. Thanks...


In reply to Reopen a closed file for read by twdgt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.