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
Reload this Page fopen help

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 10-26-2003, 02:08 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



fopen


This is the BIGGEST part I struggle with. I don't know how to do it in ANY programming language. If someone could explain to me how I would write the phrase "This is a test" to a file and then output it to the screen I'd be VERY happy You can do it in PHP or Python, doesn't matter.
__________________
--Alpha
Alpha is offline  
Old 10-26-2003, 11:36 PM   #2 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Actually not all languages use fopen for opening files. Here's a few examples of the various methods different languages use. Each example opens file.txt for appending, writes a string to it, reopens the file for reading and outputs the content 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.
????: NamePros.com http://www.namepros.com/programming/15464-fopen-help.html

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/htmlrnrn"

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/htmlnn";

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 10-27-2003, 05:21 PM THREAD STARTER               #3 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



Thanks very much! That will help me get it straight! BTW is it possible to create txt files with Python? So that if a txt file doesnt exist and you try to write to it it will be created?
__________________
--Alpha
Alpha is offline  
Old 10-31-2003, 09:06 AM   #4 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Yes Python will attempt to create the file if it doesn't exist when opened for appending or writing.
deadserious is offline  
Closed Thread


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


 
All times are GMT -7. The time now is 02:05 PM.

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