從零學Perl 檔案的操作

2021-08-20 04:00:05 字數 849 閱讀 6283

在perl中可以用

open或者sysopen

函式來開啟檔案進行操作,這兩個函式都需要通過乙個檔案控制代碼(即檔案指標)來對檔案進行讀寫定位等操作。

下面以open

函式為例:

1:讀:

open

(檔案控制代碼,"

<

檔名")/

open

(檔案控制代碼,"檔名"),前提檔案必須已經存在,否則會返回0,出錯資訊在$!中。

2:寫:

open

(檔案控制代碼

,">

檔名"),檔案如果不存在,那麼建立之,如果存在,內容被清空,長度截為

0,$!

中有出錯資訊。

3:追加:

open

(檔案控制代碼,"

>>

檔名"),基本同寫,但有一點,檔案中的內容不會被清空,新的內容會追加到原文後面。

4:讀寫:

open

(檔案控制代碼,"

+<

檔名"),通過「

+<

」模式,你可以既可以讀檔案,又可以寫檔案。你可以通過

tell()

函式在檔案內部移動,通過seek()函式進行定位。如果檔案不存在,就會被建立。如果檔案已經存在,原來的資料不會被清除。

open

(檔案控制代碼,"

+>

檔名"),通過「

+》」模式,可以同時讀寫檔案,但與上面不同的是,它是破懷性寫,會清除原來的內容。

例子:my $conf = "d:/test.txt";

if(-e $conf) 

#判斷檔案是否存在

close(file);

perl 檔案操作

perl中檔案操 基礎在perl中,提供了三種檔案控制代碼 stdin,stdout,stderr.它們可以由父程序建立的檔案或者裝置會自動開啟.一般使用open函式來建立檔案控制代碼.open函式的語法是唯讀模式時為 open filehandle,somename 其中filehandle是檔案...

perl 檔案操作

1 讀取某檔案,如果該檔案不存在,則報錯,並提示出錯原因 open db,home ellie myfile or die can t open file n 2 讀寫檔案的方法 open fh,opens filename for reading.讀 the symbol is optional....

perl的檔案操作 1

假如有乙個從web上拷貝下來的檔案 01 usr bin perl w 02 03 use tk 04 05 tk strictmotif 1 06 07 main mainwindow new 08 09 button1 main button text exit 10 command exit ...