use strict;
use FileHandle;
my $output_file = "/tmp/output_perl.txt";
if(my $fh = new FileHandle('> '.$output_file) ){
print $fh "Hello World!\nAdditional line\n";
$fh->close();
}
else{
die("Failed to write to ".$output_file);
}
if(my $fh = new FileHandle($output_file) ){
local $/ = undef;
print <$fh>;
$fh->close();
}
Python
import sys
output_file = "/tmp/output_python.txt"
try:
with open(output_file, "w") as wfh:
wfh.write("Hello World!\nAdditional line\n")
except IOError:
print("Failed to write to " + output_file)
sys.exit(1)
except:
print("Unexpected error:", sys.exc_info()[0])
sys.exit(1)
with open(output_file, "r") as rfh:
print(rfh.read(), end="")
Ruby
output_file = "/tmp/output_ruby.txt"
begin
File.open(output_file, "w") do |wfh|
wfh.puts("Hello World!\nAdditional line\n")
end
rescue SystemCallError => e
puts "class=#{e.class},message=#{e.message}"
rescue IOError => e
puts "class=#{e.class},message=#{e.message}"
end
File.open(output_file, "r") do |rfh|
print(rfh.read())
end
You can find programming examples to solve same problem using multiple programming languages.
Please find best programming language for your problems and solution.
Covering all cases: JavaScript Perl PHP Python Ruby | Covering some cases: C C++ C# Go Java Rust Shell