Solved

Overwriting a files contents:

Posted on 2002-06-18
3
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

736 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