Wednesday, January 23, 2013

Perl의 File Write

Text File 에 쓰기
 sub WriteToTextFile  
 {  
      my $outfile = shift;  
      my $text = shift;  
        
      my $errmsg = "Couldn't write to $outfile";  
      open (OUTFILE, ">$outfile") or die "$errmsg : $!";  
   
      print OUTFILE $text;  
      close OUTFILE;  
        
      print("Write success.\n");  
 }  

Binary File 에 쓰기
 sub WriteToBinFile  
 {  
      my $pa = shift;  
      my $outfile = shift;  
        
      my $buffer = "";  
      my $errmsg = "Couldn't write to $outfile";  
      open (OUTFILE, ">$outfile") or die "$errmsg : $!";  
      binmode (OUTFILE);  
        
      foreach(@$pa){  
           $buffer .= pack('C1', $_);  
      }  
   
      print OUTFILE $buffer;  
      close OUTFILE;  
        
      print("Write success.\n");  
 }  

No comments:

Post a Comment