Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Again: Opening and and writing back to same file 1

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
FR
To all,

Please excuse by previous message...I was in a big hurry and I did not detail my problem. Thanks for the interest you had.

I have the following situation. A first program creates files in directory "D". A second program reads files from "D". Unfortunatly, the script in second program cannot deal with trailing spaces, so I created a perl script to open files in "D", remove the trailing spaces and write back to a file. I want to write back to the same file, but it was crashing on the file handle (cannot write to file or something like that...). I have just tried it again on another computer (other perl?) and it works!!!

A small explanation on $line =~ s! {2,}$!!;
I use ! instead of /. This syntax replaces 2 or more trailing spaces by nothing.

Thanks to all
 
i don't know whether it matters or not but $! has a special meaning. have you tried it using different pattern delimiters?
Mike
michael.j.lacey@ntlworld.com
 
I can only think of two reasons why you would not be able to write to a file.
1 - you don't have permission to
2 - someone else (another process) has it locked - does not sound likely, unless you are doing some flocking.

about - 1 - assuming a UNIX OS
Are you sure you have write permission to the file? Who owns the file? Do you own it? Who is running the process that is trying to rewrite the file? If you own the file and the web daemon (httpd) is trying to rewrite it (or visa versa), then you may need to tweak the permissions before the file can be rewritten. You likely would be able to read it all day long, just wouldn't be able to write to it. If you don't see a problem here, repost the code and the error and we'll take another look.
I'm not sure how a WIN OS would deal with this. Maybe the creating process still has the file open?????

about - 2 -
I don't think there is much here. If you are closing the 'read' file handle before opening the same file for writing, then the Perl is going to pursue these seqeuntially and you will not have a problem.

Aside - I concur with Mike that using '!' as a delimiter is confusing.


'hope this helps.




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top