- Impact
- 6
Here's a few code snippets of the various methods different languages use for manipulating text files. Each example opens file.txt for appending, writes a string to it, reopens the file for reading and outputs the entire content of the text file to the browser. If you want to overwrite the content of the file rather than append to it you can switch the "a" to "w" for both PHP and Python and change the ">>" to ">" for Perl. There's other methods of doing this for each language, but this should give a good idea of how certain functions for each language compare to each other.
PHP
Python
Perl
PHP
Code:
<?
$file = fopen ("file.txt", "a");
fwrite ($file, "This is a PHP test<br />n");
fclose ($file);
$file = fopen ("file.txt", "r");
$lines = fread ($file, filesize ("file.txt"));
fclose ($file);
echo $lines;
?>
Python
Code:
#!/usr/bin/python
print "Content-type: text/html\n\n"
file = open('file.txt','a')
file.write("This is a Python test<br />n")
file.close()
file = open('file.txt','r')
for lines in file.readlines():
print lines,
file.close()
Perl
Code:
#!/usr/bin/perl
print "Content-type: text/html\n\n";
open (FILE, ">>file.txt");
print FILE "This is a Perl test<br />n";
close (FILE);
open (FILE, "file.txt");
@lines = <FILE>;
close (FILE);
print @lines;