Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-23 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found