开发者

Perl: Clear contents of file and open file in append mode

开发者 https://www.devze.com 2023-04-02 06:14 出处:网络
I need to open a file in append mode in Perl, but I need that before o开发者_C百科pening file all the data is deleted and fresh data goes in.

I need to open a file in append mode in Perl, but I need that before o开发者_C百科pening file all the data is deleted and fresh data goes in.

I will be entering data line by line, so before entering the first line I need all previous data is deleted.

Please help.


I think you are confused about what "append" means in perl. What you are describing is opening a file and truncating it, i.e.:

open my $fh, '>', $file;

This will delete the contents of $file and open a new file with the same name.

The reason to use open for appending is when you have a file that you do not wish to overwrite. I.e. the difference between > and >> is simply that the former truncates the existing file and begins writing at the start of the file, and the latter skips to the end of the existing file and starts writing there.

Documentation here


truncate


File handling includes:

  1. Read a file (<)
  2. Write in a file
    1. Append (>>)
    2. Overwrite (>)

For detailed explanation please visit this link.


open(fileHandle, ">", $filePath);

0

精彩评论

暂无评论...
验证码 换一张
取 消