Solved

Overwriting a files contents:

Posted on 2002-06-18
3
206 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Port 80 requests 16 101
Perl Scripting from a shell script with if and for 4 87
Linux rename files by reordering dates in the file name 6 35
perl syntax 3 17
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…

808 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