Home > Archive > PERL Beginners > October 2007 > Absolute noobie question regarding opening files on Windows platform
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Absolute noobie question regarding opening files on Windows platform
|
|
| Ayesha 2007-10-22, 7:01 pm |
| Hi all
I wrote this code to read a file (in the same directory as the script)
on Win XP
****************************************
****************************************
*******************
#!/usr/local/bin/perl
use strict;
use warnings;
open(READFILE1,"<./Sample_text_file.txt") or die ("Cannot open the
given file");
my $record;
while ($record = <READFILE1> ) {
print $record;
}
close READFILE1;
#print "Is this even working? \n"
****************************************
****************************************
********************
It is gives me output that "Cannot open the given file". However, the
same program is working on linux/mac platforms, i.e. can open files
and read them. To check that I have perl correctly installed I added a
print statement at the end. When I block everything regarding opening
the file and just have the print statement in the script, the script
works OK, implying Perl is installed correctly. Can anyone tell me
what is wrong. Can anyone tell me what am I doing wrong here? It seems
some Windows specific thing.
thanks
Ayesha
| |
| Yitzle 2007-10-22, 7:01 pm |
| The '/' is used on Unix, but not on Windows.
Try replacing it with a '\'
open(READFILE1,"<.\Sample_text_file.txt") or die ("Cannot open the given file");
Or maybe just drop the './' entirely.
| |
| Jenda Krynicky 2007-10-22, 7:01 pm |
| From: yitzle <yitzle@users.sourceforge.net>
> The '/' is used on Unix, but not on Windows.
> Try replacing it with a ''
> open(READFILE1,"<.\Sample_text_file.txt") or die ("Cannot open the given file");
> Or maybe just drop the './' entirely.
Wrong and wrong.
1) Most system calls do not give a damn whether you use forward or
backward slashes, The backward slashes are only needed if you are
passing the path to an external program, in the open() call you can
use both.
2) If you use a backslash in a doublequoted string you have to DOUBLE
IT!
Try
print "<.\Sample_text_file.txt";
You are right though that it's best to drop the ./ though.
Ayesha, are you sure the file exists? And are you sure the current
working directory is what you think it is?
use Cwd;
print "CWD: ", getcwd(), "\n";
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Rob Dixon 2007-10-22, 10:01 pm |
| Ayesha wrote:
> Hi all
>
> I wrote this code to read a file (in the same directory as the script)
> on Win XP
> ****************************************
****************************************
*******************
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> open(READFILE1,"<./Sample_text_file.txt") or die ("Cannot open the
> given file");
> my $record;
> while ($record = <READFILE1> ) {
> print $record;
> }
>
> close READFILE1;
>
> #print "Is this even working? \n"
> ****************************************
****************************************
********************
> It is gives me output that "Cannot open the given file". However, the
> same program is working on linux/mac platforms, i.e. can open files
> and read them. To check that I have perl correctly installed I added a
> print statement at the end. When I block everything regarding opening
> the file and just have the print statement in the script, the script
> works OK, implying Perl is installed correctly. Can anyone tell me
> what is wrong. Can anyone tell me what am I doing wrong here? It seems
> some Windows specific thing.
Change your open call to:
open READFILE1, 'Sample_text_file.txt' or die "Cannot open the file: $!";
and you will see additional information as to why the file couldn't be opened.
Rob
| |
| Ayesha 2007-10-23, 4:00 am |
| On Oct 22, 9:32 pm, rob.di...@350.com (Rob Dixon) wrote:
> Ayesha wrote:
>
****=AD************************[color=da
rkred]
>
>
>
****=AD*************************[color=d
arkred]
>
> Change your open call to:
>
> open READFILE1, 'Sample_text_file.txt' or die "Cannot open the file: $!=
";
>
> and you will see additional information as to why the file couldn't be op=
ened.
>
> Rob- Hide quoted text -
>
> - Show quoted text -
I was not in the right directory, but I learnt about forward and
backward slashed also. Thanks to all who replied
Ayesha
| |
| Paul Lalli 2007-10-23, 7:01 pm |
| On Oct 22, 7:49 pm, yit...@users.sourceforge.net (Yitzle) wrote:
> The '/' is used on Unix, but not on Windows.
> Try replacing it with a ''
> open(READFILE1,"<.\Sample_text_file.txt") or die ("Cannot open the given file");
> Or maybe just drop the './' entirely.
To the OP, please ignore this post completely. It is very wrong, and
will lead you to more complicated errors.
Yitzle, I'm sure you mean well, but taking random guesses at what
might be wrong, and worse, at what a solution might be, are far worse
than giving no advice at all.
Paul Lalli
| |
| Paul Lalli 2007-10-23, 7:01 pm |
| On Oct 22, 3:27 pm, ayesha.ka...@gmail.com (Ayesha) wrote:
> Hi all
>
> I wrote this code to read a file (in the same directory as the script)
> on Win XP
> ****************************************
*********************************=
**=AD************************
> #!/usr/local/bin/perl
> use strict;
> use warnings;
>
> open(READFILE1,"<./Sample_text_file.txt") or die ("Cannot open the
> given file");
> my $record;
> while ($record =3D <READFILE1> ) {
> print $record;
> }
>
> close READFILE1;
>
> #print "Is this even working? \n"
> ****************************************
*********************************=
**=AD*************************
> It is gives me output that "Cannot open the given file". However, the
> same program is working on linux/mac platforms, i.e. can open files
> and read them. To check that I have perl correctly installed I added a
> print statement at the end. When I block everything regarding opening
> the file and just have the print statement in the script, the script
> works OK, implying Perl is installed correctly. Can anyone tell me
> what is wrong. Can anyone tell me what am I doing wrong here? It seems
> some Windows specific thing.
You are asking *us* why Perl can't open the file, before you ask
*Perl* why it can't open the file. That is most illogical...
Your die() message should include the $! variable, which contains the
last operating system error. It will tell you why the file could not
be opened. Change your die() to:
die "Cannot open the given file: $!";
Not relevant to the problem at hand, but you should also be using
lexical filehandles instead of global barewords, and get into the
habbit of using the three-argument form of open:
open my $READFILE1, '<', './Sample_text_file.txt' or
die "Cannot open the given file: $!";
Paul Lalli
| |
| Paul Lalli 2007-10-23, 7:01 pm |
| On Oct 23, 12:27 am, ayesha.ka...@gmail.com (Ayesha) wrote:
> I was not in the right directory, but I learnt about forward and
> backward slashed also. Thanks to all who replied
Arg. This is exactly what I was afraid of. The post about forward vs
backwards slashes was wrong. Do not follow it. You should ALWAYS use
front slashes, regardless of Windows vs Unix. The only thing in
Windows that requires backslashes is the cmd.exe or command.com
shells. You are not using these shells when you write a Perl program.
Paul Lalli
| |
| Jenda Krynicky 2007-10-23, 7:01 pm |
| From: Paul Lalli <mritty@gmail.com>
> You are asking *us* why Perl can't open the file, before you ask
> *Perl* why it can't open the file. That is most illogical...
>
> Your die() message should include the $! variable, which contains the
> last operating system error. It will tell you why the file could not
> be opened. Change your die() to:
>
> die "Cannot open the given file: $!";
You might want to use $^E instead, under some OSes (VMS, OS/2,
Windows) it contains more details than $!, on other's it's equal to
$!.
Jenda
===== Jenda@Krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery
| |
| Ron Bergin 2007-10-25, 7:59 am |
| On Oct 23, 6:57 am, mri...@gmail.com (Paul Lalli) wrote:
> On Oct 23, 12:27 am, ayesha.ka...@gmail.com (Ayesha) wrote:
>
>
> Arg. This is exactly what I was afraid of. The post about forward vs
> backwards slashes was wrong. Do not follow it. You should ALWAYS use
> front slashes, regardless of Windows vs Unix. The only thing in
> Windows that requires backslashes is the cmd.exe or command.com
That's not entirely correct. As log as you use proper quoting, you
can use forward slashes in the cmd shell.
Try this:
C:\>cd "c:/Program Files"
C:\Program Files>
| |
| Ron Bergin 2007-10-25, 7:59 am |
| On Oct 23, 6:57 am, mri...@gmail.com (Paul Lalli) wrote:
> On Oct 23, 12:27 am, ayesha.ka...@gmail.com (Ayesha) wrote:
>
>
> Arg. This is exactly what I was afraid of. The post about forward vs
> backwards slashes was wrong. Do not follow it. You should ALWAYS use
> front slashes, regardless of Windows vs Unix. The only thing in
> Windows that requires backslashes is the cmd.exe or command.com
> shells. You are not using these shells when you write a Perl program.
>
> Paul Lalli
My first reply hasn't propagated yet, so I'll post this update.
The cmd shell excepts both types of slashes. I first thought that you
needed to use quotes when using forward slashes, but I was wrong. Try
this simple test.
C:\>notepad c:/boot.ini
| |
|
| On Oct 23, 6:55 pm, mri...@gmail.com (Paul Lalli) wrote:
> On Oct 22, 3:27 pm, ayesha.ka...@gmail.com (Ayesha) wrote:
>
>
>
>
>
>
****=AD=AD************************[color
=darkred]
>
>
>
****=AD=AD*************************[colo
r=darkred]
>
> You are asking *us* why Perl can't open the file, before you ask
> *Perl* why it can't open the file. That is most illogical...
>
> Your die() message should include the $! variable, which contains the
> last operating system error. It will tell you why the file could not
> be opened. Change your die() to:
>
> die "Cannot open the given file: $!";
>
> Not relevant to the problem at hand, but you should also be using
> lexical filehandles instead of global barewords, and get into the
> habbit of using the three-argument form of open:
>
> open my $READFILE1, '<', './Sample_text_file.txt' or
> die "Cannot open the given file: $!";
>
> Paul Lalli- Hide quoted text -
>
> - Show quoted text -
Some suggestions:
1=2E Instead of using $! for displaying error messages, use $^E. U will
get more descriptive error messages.
2=2E Quickly going thru this digest i observerd usage of '' in double
quotes, this is not recommended. always escape '' in double quotes
like this "\\". Heres an example where things can go wrong, lets say
ur file/directory name starts with 'n' and u say somthing like open
FILE, "<.\newFile"; "\n" is interpreted as special character by perl
or for that matter any other language. Correct way would be "open
FILE, ".\\newFile";
~emptee.
| |
| Ron Bergin 2007-10-26, 7:01 pm |
| On Oct 25, 9:38 pm, Mayur.Thak...@gmail.com (mAyur) wrote:
> On Oct 23, 6:55 pm, mri...@gmail.com (Paul Lalli) wrote:
>
>
>
>
>
******=AD=AD************************[col
or=darkred]
>
>
[snip unrelated stuff]
>
>
>
> Some suggestions:
> 1. Instead of using $! for displaying error messages, use $^E. U will
> get more descriptive error messages.
> 2. Quickly going thru this digest i observerd usage of '' in double
> quotes, this is not recommended. always escape '' in double quotes
> like this "\\". Heres an example where things can go wrong, lets say
> ur file/directory name starts with 'n' and u say somthing like open
> FILE, "<.\newFile"; "\n" is interpreted as special character by perl
> or for that matter any other language. Correct way would be "open
> FILE, ".\\newFile";
>
> ~emptee.
Your #2 suggestion is not the best in this case/example.
Why did you use a forward slash instead of backslash for the path
separator?
Better advise would be to:
1) Use double quotes (or the qq() operator) only when needed i.e.,
when you need variable interpolation.
2) Use the 3 arg form of open as Paul showed.
| |
| Dr.Ruud 2007-10-28, 7:00 pm |
| mAyur schreef:
> always escape '' in double quotes like this "\\".
Inside single quotes or q{} too. Try for example:
perl -wle '
print q{\}
'
--
Affijn, Ruud
"Gewoon is een tijger."l
|
|
|
|
|