Skip navigation

Page Resource Logo


Writing to Files

How to write to a file and how to delete a file
In the last tutorial, we saw how to append data to a file. Here we are going to look at writing to a file and how to delete a file.

The File

Writing to a file erases everything that was in the file previously and writes the new information over it. Before you try this on any important information, you should create a test file to make sure these commands work properly. Also, back up your data first just in case.

In the last section, we added a line to our "websites.cgi" file by appending the file. So now our file would have three lines:

PageResource|http://www.pageresource.com|Tutorials
JavaScript City|http://www.javascriptcity.com|Scripts
Mysite|http://www.mysite.com|My website

So, what if we decided that we didn't want those sites anymore, but wanted to replace them with a new site we found (ouch!). We could do it by opening the file for writing, which will overwrite the file contents with whatever we put into it.

Writing (Overwriting)

We open the file for writing much like we have before, but this time we use just one > symbol before the file variable, rather than the two we used for appending:

open(DAT,">$sitedata") || die("Cannot Open File");

After that, we just write in our new file contents and close the file (For information on the file locking portion, see the previous tutorial. You may also need to adjust the file permissions so that you can write to the file. See the CHMOD page for more on that):

use Fcntl qw(:flock :seek);

$sitename="Supersite";
$siteurl="http://www.supersite.com";
$description="A Super Duper Site";
$sitedata="websites.cgi";

open(DAT,">$sitedata") || die("Cannot Open File");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);
print DAT "$sitename\|$siteurl\|$description\n";
close(DAT);

Now, the file would have only our new contents in it, and none of the old contents will remain:

Supersite|http://www.supersite.com|A Super Duper Site

Nice, but what if we just wanted to delete one of the lines from the file? We will still have to overwrite it, but with a little twist.

Deleting One Line

Deleting one line is a little more difficult, since there is no command for it directly. What we have to do is read the contents of the file to an array, splice the line we want to delete from the array, and write the contents of the array back to the file. If we go back to our 3-line file at the beginning of this section, this will overwrite what we had (3 lines) with what we send back (2 lines). So, if we want to delete the second line in the file, here is the code:

use Fcntl qw(:flock :seek);

$sitedata="websites.cgi";

open(DAT, $sitedata) || die("Cannot Open File");
@raw_data=<DAT>;
close(DAT);

splice(@raw_data,1,1);

open(DAT,">$sitedata") || die("Cannot Open File");
flock(DAT, LOCK_EX);
seek(DAT, 0, SEEK_SET);
print DAT @raw_data;
close(DAT);

The first part you will remember from the reading files section, we read the file into an array. Then you will see the splice command. It says to take out the line with the array number of 1, and to do this for one element (line) in the array. Remember, an array starts counting at zero, so the first line of the file is zero, the second is 1, and the third line is 2. Since we wanted to remove the second line, we chose 1. The last section opens the file for writing and writes the contents of the array into the file, which overwrites it with only our two remaining links.

After that, the file should look like this:

PageResource|http://www.pageresource.com|Tutorials Mysite|http://www.mysite.com|My website

Now that we have that, let's see how to just get rid of the file entirely:

Deleting the File

If we want to just delete a file for good and make it no longer exist, we can do that with a short command:

unlink("file_location");

We replace that with a filename to delete:

unlink("filename.cgi");

In the case of our file, we assigned the filename to a variable, so we can use the variable instead:

$sitedata="websites.cgi";
unlink($sitedata);

Just remember, the file is gone afterward, so you might want to keep a backup if you write a script for data you may need again.

So, that takes care of that, just remember to be careful with these as they overwrite or delete data and files entirely. Better to keep some backups and be on the safe side than to lose data you need (I've done it a few times, and wasn't a happy camper!).

Well, that's all for now, let's go on to: File Checks.

 



Topics: HTML & CSS | JavaScript | CGI & Perl | ASP/PHP | DHTML | Java | Contact Us | Privacy Policy

Copyright © 1997-2010 The Web Design Resource. All rights reserved. Disclaimer.