NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Opening a text file and displaying the results in browser with PHP, Perl, and Python

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 12-29-2003, 01:47 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Opening a text file and displaying the results in browser with PHP, Perl, and Python


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
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
????: NamePros.com http://www.namepros.com/code/16010-opening-text-file-displaying-results-browser.html
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;
deadserious is offline  
Old 05-15-2004, 08:58 PM   #2 (permalink)
Account Closed
 
axilant's Avatar
Join Date: May 2004
Location: /etc/passwd
Posts: 2,178
axilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to beholdaxilant is a splendid one to behold
 

Member of the Month
July 2005

Thanks... i'm learning perl
axilant is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 07:26 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger