Solved

Overwriting a files contents:

Posted on 2002-06-18
3
202 Views
Last Modified: 2010-08-05
I have a file which I lock and open, and i have set the open for input and output, assuming that since I didn't set it as append when I print to the file it would overwrite the current contents, sadly this is not the case...

So, what I need to do is open the file, read the contents into an array and then print a new set of contents over the top of what was there... is there a way I can do this without closing the file again the opening it as write only and printing, as I do not want to lose my lock on the file!

The script that is running is highly likely have multiple instances occuring and I cannot risk these instances getting data at inapropriate times...

Hope you can help
Regds
JB

0
Comment
Question by:JavaBloggs
  • 2
3 Comments
 

Author Comment

by:JavaBloggs
ID: 7088125
Minor ammendment to this question:

I have sorted my code, and now have a lock and open and lock and reopen statement, so all I realy need to do is get my IO symbols sorted!!! so could you kindly confirm the following:

    open(THISFILE, "< /wibble/thing.dat");
    will allow me to open a file and read its contents into and array.

    open(THISFILE, "> /wibble/thing.dat");
    will allow me to open a file for writing, and print over existing contents (not append).

I suppose the question I a realy asking is if I used something like the following:

  open(DATAFILE, "> info.dat");
    foreach $content (@contents) {
      print DATAFILE $content\n;
    }
  close(DATAFILE);

would it completely overwrite the contents of info.dat with the contents of the @contents array with one entry on each line? or would the pervious entries in the file survive?

Regds
JB
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 100 total points
ID: 7091244
Yes, the ">" will cause any existing file of that name to be overwritten. Make sure you first close the file handle you opended for reading with <.
0
 

Author Comment

by:JavaBloggs
ID: 7092180
Cheers is apprieciated :o)
Sorry about the dumbness of this one, but its been a while, and am working on a live system  :o(

regds
JB
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now