Solved

Writing to a file problems..

Posted on 1998-06-22
9
159 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 2

Expert Comment

by:tpryor
ID: 1208016
have perl create the file, that way it could write to it.
0
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 157
work on ods spreadsheet with perl in ubuntu 4 76
Port 80 requests 16 95
Problem with one substitution modifying another 3 68
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

13 Experts available now in Live!

Get 1:1 Help Now