Solved

Writing to a file problems..

Posted on 1998-06-22
9
157 Views
Last Modified: 2010-03-05
OK, I wrote this program which needs to write out to a file, and it worked initially.  But for some reason, it won't
work now.  I didn't change the file at all, and it just stopped writing to the file.  It was still running and doing everything correctly, except opening a file for writing.

I've tried several things, and wrote a simplified version for easier testing, and found that my server wasn't allowing the script to open a file for writing when called from a web page (a form submission), but it worked fine when
I ran it from the command line.

The files have 775 permissions.  Any ideas?  Here's the test script I wrote:

#!/usr/bin/perl
# print the header to start the output page
print "Content-type: text/html\n\n";
print "<HTML><BODY>\n";

open(IN, "week-3-7--3-13.txt");             # Open the file
@lines = <IN>;               # Read it into an array
close(IN);                   # Close the file

open(OUT, ">newfile.txt");          # Open the file
print OUT "@lines";             # Write updated scores to the file
close(OUT);                   # Close the file

print "@lines";
print "</BODY></HTML>";
0
Comment
Question by:jbirk
9 Comments
 
LVL 5

Expert Comment

by:b2pi
ID: 1208013
Most likely, your server is running the script as a user other than you (like nobody).  Try setting default permissions to 777.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208014
Oops, too quick on the trigger.... That should be 666 for your data files.



By the way, you can save yourself the points by checking out http://www.perl.com/CPAN-local/doc/FAQs/cgi/idiots-guide.html


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208015
open(OUT, ">newfile.txt") or print "can't open newfile.txt because $!";
#might give you a hint to what perl thought the problem was.
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1208016
have perl create the file, that way it could write to it.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 8

Author Comment

by:jbirk
ID: 1208017
Changing the mod to 666 didn't help.  I'm going to check out ozo's suggestion.
And I also tried tpryor's suggestion before asking the question.  It won't create the file either??!???
0
 
LVL 8

Author Comment

by:jbirk
ID: 1208018
OK, I jumped the gun.  After playing using ozo's suggestion, I got the message 'Permission Denied'  So I though about it, and it seemed like b2pi had to be right, but the files had write permission.  It was the fact that it didn't even create files that tiped me off to the solution.  The directory needed write permission as well.  For some reason the system changed a couple weeks ago, and the permissions where affected somehow (because it DID work fine before).  But now with the directory AND file having write permission to everyone, it works again.

I'm not sure how to give the credit here.  You all provided clues to the solution.  Any suggestions?  Any way to split up points?

I really think they should change the system so that the asker of the question could assign points to the people writing the comments as they see fit after a satisfactory solution has been found (then splitting points would be easy! and people wouldn't accidentally answer questions instead of commenting).

Anyway, thank you all for your help!
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1208019
Was it that you didn't have write rights to the _directory_ ?
0
 
LVL 8

Author Comment

by:jbirk
ID: 1208020
yes that's correct.  You sort of answered with that solution b2pi, but you weren't specific enough for me to realize that right away.  I just thought of the file itself, and forgot about the directory.

I guess if you answer the question again, I can give you the points, but a B for the grade, since it was the right solution, just not stated clearly enough.

But I'd like to give some points to the others as well, since helped me figure it out as well.
(how do I do that?)

Thanks for the help!
0
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
ID: 1208021
I'm relatively new on the system, and can't really tell you how to split the points.  Sorry.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now