The code you show doesn't seem relevant to your question, as that code is reading from one file and writing to a different one. I think you're asking about reading and writing to the same file. If you tried it and it's not working, it would have been nice if you had posted the code you used in your attempt, and an explanation of what "it is not working" means to you (and what you want it to mean to me).

That said, if you really are trying to modify the same file that you're reading from, and your modifications change the file's size (as they do), you've got a real headache of a strategy. You are much better off keeping the current strategy of opening one file for input, and one for output. Once you've closed the files you can rename the input file to something like 'main.c.bak, and rename the output file to the name of the original input file.

Also, save yourself some trouble: Be sure to put an or die $!; clause to the end of each of your open statements, as well as to the close statement for the output file. Let the computer tell you when things don't work, and why. Don't just assume that system calls executed without error.


Dave


In reply to Re: modifying in the same file by davido
in thread modifying in the same file by prassi

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.