Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

modifying in the same file

by prassi (Acolyte)
on Jun 08, 2012 at 18:03 UTC ( #975215=perlquestion: print w/replies, xml ) Need Help??
prassi has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl Monk,

I am intend to remove C and C++ style comment from a C code. In the below perl script I am reading from a file, modifying and writing into another file.

Now my question I need to write into the same file as when I modify line. If I open the file in write mode it is not working. Please help me how do I write into the same file and at the same line.

#!/bin/perl5.8.6 $flag = open(file1, "main.c"); $flag2 = open(file2, ">main_out.c"); if(flag && flag2){ while(!eof(file1)){ $/ = undef; $_ = <file1>; s#(/\*.*?\*/)|//[^\n]*|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|.[^/"'\\]*)# +defined $2 ? $2 : ""#gse; print file2 ($_); } } close(file1); close(file2);

Regards, =Prasad

Replies are listed 'Best First'.
Re: modifying in the same file
by Illuminatus (Curate) on Jun 08, 2012 at 19:04 UTC
    zentara did indeed answer the question you asked, but it's probably not the question that you wanted to ask :) If you are really asking "how can I open the file, remove the comments, and then close it (ie not create an additional file)", the answer is that, in general, you can't. If you read the section on open he quoted, it tells you so. You can use perl -i, which says it modifies the file in place, but if you read the fine print, you'll see that it actually uses 2 files. There are ways to use just one file, but that is an exercise left to the user


Re: modifying in the same file
by davido (Archbishop) on Jun 09, 2012 at 00:15 UTC

    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.


Re: modifying in the same file
by zentara (Archbishop) on Jun 08, 2012 at 18:39 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://975215]
Approved by davido
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2017-03-30 02:08 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (353 votes). Check out past polls.