Contributed by Anonymous Monk
on Apr 28, 2000 at 21:59 UTC
Q&A
> input and output
Description: using perl, of course. no one here at work listens
to me when i try to get them to take off the spaces in filenames. Answer: How do you open a filehandle to a DOS file which has spaces in the filename? contributed by grinder This is not a problem unique to DOS, all operating systems are prone to this problem. What is worse, if the file has leading spaces, Perl will silently strip those spaces out, causing the call to fail.
If you don't believe this, try running the following commands (under Unix).
% echo foo >' leading space';
% perl -le 'open I, " leading space" or print "bletch: $!"'
This outputs bletch: No such file or directory.
This is due to the way Perl looks for < and > meta-characters to determine what mode to use (input or output). The most reasonable portable and documented method to work around this bug is to use sysopen in the place of open. In any event, you will need to use the Fcntl module to get this working correctly (part of the core distribution).
Here are the most common cases to get you started:
| mode | open | sysopen |
| input |
open IN, 'a file.txt' |
sysopen IN, 'a file.txt', O_RDONLY |
| output |
open OUT, '>a file.txt' |
sysopen OUT, 'a file.txt', O_WRONLY |
| output append |
open OUT, '>>a file.txt' |
sysopen OUT, 'a file.txt', O_WRONLY|O_APPEND |
Just slap a use Fcntl at the top of your program, and drop in the sysopen replacements for each open statement, remembering of course to test the truth (success) of the statement and printing the contents of $! when things go awry.
See also Two-arg open() considered dangerous. | Answer: How do you open a filehandle to a DOS file which has spaces in the filename? contributed by turnstep Just quote it and everything will be fine:
open (SPACEY, "this file has spaces.txt") || die;
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|