CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
creating a new text file

 



Killian
Deleted

May 2, 2001, 12:41 AM

Post #1 of 14 (3169 views)
creating a new text file Can't Post

I made a script that is supposed to, check to see if a txt file exists, if it doesn't then it is supposed to create a new txt file and put various information in it. I tried opening it like so:

open (FILEHANDLE, ">path/to/file.txt");
print FILEHANDLE "this oes to the file";
close (FILEHANDLE);

afterwards its supposed to print a message, what happens is it displays the message, but it doesn't create the file, I can't see anything wrong with the code, could there be another reason why it's not creating the file?



Jean
User


May 2, 2001, 4:30 AM

Post #2 of 14 (3166 views)
Re: creating a new text file [In reply to] Can't Post

Try printing the system error ($!) in case the file is not created
(this may give you a clue to what's actually wrong), in the following way:
open (FILEHANDLE, ">path/to/file.txt") or die "Error creating file - $!\n";
Another thing: I hope you're not testing for file existence by opening the file with (>) - this will erase all the file contents. Even if you aren't, the best way to test for file existemce is (-e $filename)

Hope this helps,

Jean Spector
QA Engineer @ Extent Technologies, Ltd.
mage@lycosmail.com


Killian
Deleted

May 2, 2001, 11:47 AM

Post #3 of 14 (3154 views)
Re: creating a new text file [In reply to] Can't Post

ok, i put in this code:

if (-e "path/to/file.txt")
{
print "That file lready exists.";
}
else
{
open (FILE, ">path/to/file.txt") or die "Error creating file - $!\n";
print FILE "put this in line one of the txt file\n";
print FILE "put this in line 2 of the txt file\n";
close (FILE);
print "the file has been created.";
}

but when i do it, it doesn't show anything, it doesn't have any error message at all and it doesn't say "the file has been created." but the txt file still isn't there! :(



Killian
Deleted

May 3, 2001, 5:18 PM

Post #4 of 14 (3140 views)
Re: creating a new text file [In reply to] Can't Post

can anyone help me?



rGeoffrey
User / Moderator

May 3, 2001, 6:50 PM

Post #5 of 14 (3138 views)
Re: creating a new text file [In reply to] Can't Post

The problem could be that the program does the print, perl holds it in a buffer to make the printing more efficient, but before it gets around to printing, the program dies and the buffers die with it.

Try adding these lines near the top of your program, before you do the printing.


Code
select (STDERR); $| = 1; 
select (STDOUT); $| = 1;

This will turn off buffering and when you do a print the output will happen immedeately.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


Killian
Deleted

May 5, 2001, 10:22 PM

Post #6 of 14 (3122 views)
Re: creating a new text file [In reply to] Can't Post

that didn't help, It still doesn't do anything. :(

Do I have to do something to allow me to create new files in the directory or something?



Pro_4
User

May 6, 2001, 6:09 AM

Post #7 of 14 (3120 views)
Re: creating a new text file [In reply to] Can't Post

Just wondering did you chmod your directory 777. That was my problem when adding new files to a directory, etc.

Hope that helps :)

People Engrossed by a Reliable Language


Killian
Deleted

May 6, 2001, 10:41 AM

Post #8 of 14 (3115 views)
Re: creating a new text file [In reply to] Can't Post

What do you mean? I dont know what it means to "chmod your director 777"

That might be the reason though...



rjoseph
Novice

May 6, 2001, 4:22 PM

Post #9 of 14 (3111 views)
Re: creating a new text file [In reply to] Can't Post

What platform (operating system) are you programming on?

Because in UNIX/Linux/*NIX clones your directory has write permissions that specify what types of users can write files to that directory - 0777 allows everyone to read, write, and excute from that directory.

Say that you were running your scripts in your home directory that was /home/rjoseph - if you were in this directory, and have the proper access, just do this:

$ chmod 0777 /home/rjoseph

That should help - of course, you'll have to do a little thinking to figure out exactly what you need to do for your platform (FYI: none of this is needed on Windows platforms).

Hope that helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Killian
Deleted

May 7, 2001, 11:17 PM

Post #10 of 14 (3098 views)
Re: creating a new text file [In reply to] Can't Post

I'm pretty sure it's a unix server but I dont quite understand what your talking about.

The scripts are in /cgi-bin/ and I want to have it create a file in /cgi-bin/createfilehere

$ chmod 0777 /cgi-bin
$ chmod 0777 /cgi-bin/createfilehere
and $ chmod 0777 /createfilehere

and every time it just says internal server error



rjoseph
Novice

May 8, 2001, 12:18 PM

Post #11 of 14 (3087 views)
Re: creating a new text file [In reply to] Can't Post

Then I have a feeling that something else is wrong with your script - trying attaching the whole script so we can see what might be going wrong.

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Killian
Deleted

May 8, 2001, 9:47 PM

Post #12 of 14 (3080 views)
Re: creating a new text file [In reply to] Can't Post

I'm trying to set up a signup form that will let people create their own "accounts" by making a text file for each person(is this called a flatfile database?) and storing the info they gave in the text file, one line per form field.

I'm going to be using this in my online game if i can get it working, of course I will try to make the code better if possable, and is flatfile a good way to do an online game database or should i use something else?

here is the code, the current signup form I'm using consists of just username and password, although when i get it working and i get my game set up, I will obviously add more things.


#!/usr/bin/perl -w
print "Content-type:text/html\n\n";


if ($ENV{'REQUEST_METHOD'} eq "GET")
{
$in = $ENV{'QUERY_STRING'};
}
else
{
$in = <STDIN>;
}

print "<html><head><title>Online Game</title>
<STYLE type\=text/css>
tbody \{font\: 8pt Verdana\; color\: dddddd\;\}
body \{font\: 8pt Verdana\; color\: dddddd\;\}
td \{font\: 8pt Verdana\; color\: dddddd\;\}
tr \{font\: 8pt Verdana\; color\: dddddd\;\}
p \{font\: 8pt Verdana\; color\: dddddd\;\}
A \{text\-decoration\: none\; COLOR\: bbbbbb\;\}
A\:hover \{text\-decoration\: underline\; color\: ccccccc\;\}
</STYLE>
</head>";
print "<body bgcolor=000000 text=dddddd>";


@values = split(/&/, $in);

foreach $data (@values)
{
($name, $value) = split(/=/, $data);
$value =~ s/%(..)/pack("c",hex($1))/ge;
$value =~ s/\+/ /g;
$signup_info{$name} = $value;
}


if (-e "user_data/$signup_info{username}.txt")
{
print "I'm sorry but someone has already chosen that username.";
}
else
{
open (FILE, ">user_data/$signup_info{username}.txt") or die "Error creating file - $!\n";
print FILE "$signup_info{username}\n";
print FILE "$signup_info{password}\n";
close (FILE);
print "you have signed up.";
}

print "</body></html>";

That's my code.

every time i type in a username/password into my page it just comes up blank, no text at all.

Also, the script is in folder cgi-bin and there is a user_data folder in there where the users txt files go, the host I'm using lets you use cgi script anywhere, if that is of any help.



rjoseph
Novice

May 9, 2001, 9:49 AM

Post #13 of 14 (3075 views)
Re: creating a new text file [In reply to] Can't Post


In Reply To
@values = split(/&/, $in);

foreach $data (@values)
{
($name, $value) = split(/=/, $data);
$value =~ s/%(..)/pack("c",hex($1))/ge;
$value =~ s/\+/ /g;
$signup_info{$name} = $value;
}

DO NOT USE THIS! - I don't know how many times I have to tell people this, but you should not be using your own homespun (or someone's that you stole) code to parse CGI data!!!! There are so many security considerations that you dont take into account that you need to! Fortunatley, a very smart man named Lincoln Stien has wrriten a standard module (as of 5.005_03 I believe) - the CGI module - that is not only easy to use, but is incredibley stable and tested - please please please please please consider learning this module (note: this is not a flame, I am just trying to make sure that people know what to do :).

Secondly, you might want to try using +> to write to your file instead of just > - +> is much more forceful in creating files. Give it a shot, it might help!

Hope this helps!

r j o s e p h
"Violence is a last resort of the incompetent" - Foundation


Killian
Deleted

May 9, 2001, 12:03 PM

Post #14 of 14 (3070 views)
Re: creating a new text file [In reply to] Can't Post

hmmmm... its still not working.

I dont know what modules are, could u explain what they are/how you use them?


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives