?
Solved

Overwriting a files contents:

Posted on 2002-06-18
3
Medium Priority
?
209 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 400 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Six Sigma Control Plans
Suggested Courses

743 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