Perl語言入門--3--文件讀取與寫入


現有文件test.txt,內容為:"123\n456"

1,打開文本test.txt

      #!/usr/bin/perl

      open d,"test.txt";

    d 為文件句柄,指向打開的文件

 

2,逐行讀取文本test.txt

      #!/usr/bin/perl

      open d,"< test.txt";

      while(<d>){

          print $_;

       }

       close d;

      結果:123

               456

     或者:print <d>;

     結果:同上

     <> 為取行操作符

 

3,向test111.txt中寫入內容

   #!/usr/bin/perl

    open d,">test111.txt";

    print f "hello,world\nsee you\n";

    close d;

    如果test111.txt原本有內容,則原內容將會被擦除,test111.txt的內容為

                    hello,world 

                    see you

4,向test111.txt中追加內容

    #!/usr/bin/perl

      open d,">>test111.txt";

      print d "thank you\n";

      close d;

      test111.txt的內容為 

              hello,world

              see you

              thank you

     

   print 相當於寫命令,別丟了語句結尾的 “;"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM