Solved

Overwriting a files contents:

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 153
Perl Awk Need Help 3 109
Writing a parser for java language 4 71
Regex rule to match two different url 5 60
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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

13 Experts available now in Live!

Get 1:1 Help Now