2012-10-08 57 views
0

我有一個原始文件A.txt。 我寫信給A_Copy.txt。 我想知道如果我可以在關閉之前和關閉它之後在相同的程序中讀取A_Copy.txt? 我想在同一個程序中使用Tie::File修改A_Copy.txt在Perl程序中寫入和讀取文件

A - > A_Copy.txt

讀A_Copy.txt關閉

修改A_Copy.txt

+0

..使用'與-r'你'文件「..如果你想做任何事情..給我們一些代碼.. –

回答

2

使用拷貝後,關閉

讀A_Copy.txt之前應該足夠,以確保舊文件是可讀的,並且新文件是可寫和可讀的,副本執行得很好。 要檢查文件是否可讀的最好方法是使用-r如果你想知道文件是否可讀與否,它使用的系統調用統計

#!/usr/bin/perl 

use File::Copy; 
use Tie::File; 

my $i = "A.txt";  # input file 
my $o = "A_Copy.txt"; # output file 
my @a;    # array to use with tie 

copy($i, $o) or die; 

# check that the new file is readable, actually unneeded since copy 
# would fail on any error 
die unless (-r $o); 

# fill an array with the lines of the new file 
tie @a, "Tie::File", $o or die; 
# change the first line of the new file 
$a[0] = "Hi";