Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > write to file not working

Reply
Thread Tools

write to file not working

 
 
rxl124@hehe.com
Guest
Posts: n/a
 
      02-12-2004
someone please please help w/ this one.
As I been working on this on and off and it just does not want to work.

1 #!/usr/bin/perl -w
2
3 $file = "/home/user1/dothis";
4 open(FILE, ">$file");
5 while($line = <FILE>) {
6 if ($line =~ /B/) {print FILE "A"};
7 if ($line =~ /A/) {print FILE "B"};
8 }

and when i run above program, i get below error,


Filehandle FILE opened only for output at ./pro1 line 5.

Can someone please tell me what I am doing wrong?
Or is this bug?

Please tell me.

thanks
 
Reply With Quote
 
 
 
 
nobull@mail.com
Guest
Posts: n/a
 
      02-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> someone please please help w/ this one.
> As I been working on this on and off and it just does not want to work.
>
> 1 #!/usr/bin/perl -w
> 2
> 3 $file = "/home/user1/dothis";
> 4 open(FILE, ">$file");
> 5 while($line = <FILE>) {
> 6 if ($line =~ /B/) {print FILE "A"};
> 7 if ($line =~ /A/) {print FILE "B"};
> 8 }
>
> and when i run above program, i get below error,
>
>
> Filehandle FILE opened only for output at ./pro1 line 5.
>
> Can someone please tell me what I am doing wrong?


Is this a trick question?

According to the error message you are trying, at line 5, to read form
filehandle FILE that you opened only for output. Looking at the code
would indeed confirm that's what you are doing.

> Please tell me.


OK, other things you are doing wrong:

Posting to a non existant newsgroup (see FAQ).

Posting with a subject "write to file not working" and example code
that's crashing out on a read from file.

Not explaining what you expect you code to do (so we can't give you
any idea how fix it).

Not checking for errors from open().

Not declaring all your variables as lexically scoped in the smallest
applicable scope.
 
Reply With Quote
 
 
 
 
Jeff Dunn
Guest
Posts: n/a
 
      02-12-2004
(E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> someone please please help w/ this one.
> As I been working on this on and off and it just does not want to work.
>
> 1 #!/usr/bin/perl -w
> 2
> 3 $file = "/home/user1/dothis";
> 4 open(FILE, ">$file");
> 5 while($line = <FILE>) {
> 6 if ($line =~ /B/) {print FILE "A"};
> 7 if ($line =~ /A/) {print FILE "B"};
> 8 }
>
> and when i run above program, i get below error,
>
>
> Filehandle FILE opened only for output at ./pro1 line 5.
>
> Can someone please tell me what I am doing wrong?
> Or is this bug?
>
> Please tell me.
>
> thanks



You are trying to read from a file you have requested to opened for writing.
Try this
open(FILE, "+<$file");
 
Reply With Quote
 
rxl124@hehe.com
Guest
Posts: n/a
 
      02-13-2004
(E-Mail Removed) (Jeff Dunn) wrote in message news:<(E-Mail Removed). com>...
> (E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> > someone please please help w/ this one.
> > As I been working on this on and off and it just does not want to work.
> >
> > 1 #!/usr/bin/perl -w
> > 2
> > 3 $file = "/home/user1/dothis";
> > 4 open(FILE, ">$file");
> > 5 while($line = <FILE>) {
> > 6 if ($line =~ /B/) {print FILE "A"};
> > 7 if ($line =~ /A/) {print FILE "B"};
> > 8 }
> >
> > and when i run above program, i get below error,
> >
> >
> > Filehandle FILE opened only for output at ./pro1 line 5.
> >
> > Can someone please tell me what I am doing wrong?
> > Or is this bug?
> >
> > Please tell me.
> >
> > thanks

>
>
> You are trying to read from a file you have requested to opened for writing.
> Try this
> open(FILE, "+<$file");



THANK YOU Jeff,

w/ your help, I was able to write this little program which basically
just does one thing. It will record which server I have to work on(no
I can't do this w/ cron. as it has to be done manually and also i am
trying to learn perl here).

#!/usr/bin/perl -w
open(FH, "+<server_watch") || die ;
$count = <FH>;
if ($count == 1) {$count=2 ;}
else {$count=1;}
seek(FH, 0,0) || die;
print FH "$count";
close(FH);

Thank you so much!!!!!
Also, if anyone knows that there are already written little scripts
like this. please send me some URL. I like to look at other people's
codes...... to learn.
 
Reply With Quote
 
rxl124@hehe.com
Guest
Posts: n/a
 
      02-13-2004
(E-Mail Removed) (Jeff Dunn) wrote in message news:<(E-Mail Removed). com>...
> (E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> > someone please please help w/ this one.
> > As I been working on this on and off and it just does not want to work.
> >
> > 1 #!/usr/bin/perl -w
> > 2
> > 3 $file = "/home/user1/dothis";
> > 4 open(FILE, ">$file");
> > 5 while($line = <FILE>) {
> > 6 if ($line =~ /B/) {print FILE "A"};
> > 7 if ($line =~ /A/) {print FILE "B"};
> > 8 }
> >
> > and when i run above program, i get below error,
> >
> >
> > Filehandle FILE opened only for output at ./pro1 line 5.
> >
> > Can someone please tell me what I am doing wrong?
> > Or is this bug?
> >
> > Please tell me.
> >
> > thanks

>
>
> You are trying to read from a file you have requested to opened for writing.
> Try this
> open(FILE, "+<$file");



THANK YOU Jeff,

w/ your help, I was able to write this little program which basically
just does one thing. It will record which server I have to work on(no
I can't do this w/ cron. as it has to be done manually and also i am
trying to learn perl here).

#!/usr/bin/perl -w
open(FH, "+<server_watch") || die ;
$count = <FH>;
if ($count == 1) {$count=2 ;}
else {$count=1;}
seek(FH, 0,0) || die;
print FH "$count";
close(FH);

Thank you so much!!!!!
Also, if anyone knows that there are already written little scripts
like this. please send me some URL. I like to look at other people's
codes...... to learn.
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      02-13-2004
(E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> I was able to write this little program which basically
> just does one thing. It will record which server I have to work on(no
> I can't do this w/ cron. as it has to be done manually and also i am
> trying to learn perl here).
>
> #!/usr/bin/perl -w
> open(FH, "+<server_watch") || die ;
> $count = <FH>;
> if ($count == 1) {$count=2 ;}
> else {$count=1;}
> seek(FH, 0,0) || die;
> print FH "$count";
> close(FH);


That's not bad at all, my only criticisms are quite minor because is a
"quick ('n' dirty)" program. There are a number of techniques used in
your code that are great in a "quick 'n' dirty" program such as this
but would be bad in a real one. Since you say you want to learn Perl
I'll point them out.

Even in a quick program you should incude the error in your error
messages!

open(FH, "+<server_watch") || die $!;

In serious programs you'd probably want to include more information:

open(FH, "+<server_watch") || die "Error opening server_watch: $!";

Note, since this is a quick program I'd have not bothered with the
explicit close().

In your program you use implicitly declared global variables. For
programs below a certain size this can be a saving. For larger
programs in becomes a pain. People disagree about where the line at
which it becomes a pain is drawn but most would agree that by the time
you get to 100 lines and half a dozen subroutines you are well past
it. I, personally, am a very inaccurate typist so I draw the line at
~10 lines.

However, if you are writing small programs not as quick programs in
their own right but as a exercises in learning how to write real
programs you should get into the habit of putting "use strict;" at the
top of even your shortest programs and always declaring all variables
as lexically scoped (using my) in the smallest applicable scope
(unless there's a reson to do otherwise).

In the case of your program there's only one variable so:

$count = <FH>;

Would become

my $count = <FH>;

Putting "use strict" will disable the implicit global variable
declaration, it also disables two other features that you will rarely
need and you really don't want enabled until you've understood what
they do. (For details RTFM).

Bare filehandles, IMHO, are also a feature that's best restricted to
"quick" programs.

In larger programs you are better off using a lexical variable.

open(my $FH, "+<server_watch") || die "Error opening server_watch:
$!";

IMNSHO, the 2-arg open() in which the mode and file paramters are
concatenated is largely a legacy feature. If you are just starting
Perl now you may as well go straight to the newer syntax.

open(my $FH,'+<','server_watch') || die "Error opening server_watch:
$!";

In larger programs you should always use the more flexible "use
warnings" rather than the "-w" switch. In "quick" programs where the
few keystokes is significant then use the -w by all means.

In real (as opposed to Q&D) programs you should import the SEEK_SET
constant from the Fcntl module rather than assume it is 0. That said
I don't think there are any platforms on which Perl is ever likely to
be implemented on which it is anything else!

Oh and you may also wish to see FAQ: What's wrong with always quoting
"$vars"?

> Also, if anyone knows that there are already written little scripts
> like this.


The point about Q&D programs is you write them quickly to do one
thing, there would be no point publishing them.

Perhaps you should be looking at early examples in a Perl tutorial.

Be aware that there are many bad tutorials out there. Most (but not
all) of the bad ones can be spotted by the following features:

They don't "use strict" and "use warnings".

They either don't declare variables using my() or
don't do so in the smallest applicable scope.

They use & on all subroutine calls.

> I like to look at other people's codes...... to learn.


If you really want to look at other people's first attempts you should
consider looking in some of the Perl newsgroups that exist in this
hierachy (as opposed to this one that does not). That way you not
only see the code but also the critique. Just looking at other
people's first attempt would be a case of people who don't know what
they are doing pooling their ignorance. Of course, if you really want
to pool ignorance about Perl there's a newsgroup specially for that
too: alt.perl!
 
Reply With Quote
 
rxl124@hehe.com
Guest
Posts: n/a
 
      02-16-2004
(E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> (E-Mail Removed) wrote in message news:<(E-Mail Removed). com>...
> > I was able to write this little program which basically
> > just does one thing. It will record which server I have to work on(no
> > I can't do this w/ cron. as it has to be done manually and also i am
> > trying to learn perl here).
> >
> > #!/usr/bin/perl -w
> > open(FH, "+<server_watch") || die ;
> > $count = <FH>;
> > if ($count == 1) {$count=2 ;}
> > else {$count=1;}
> > seek(FH, 0,0) || die;
> > print FH "$count";
> > close(FH);

>
> That's not bad at all, my only criticisms are quite minor because is a
> "quick ('n' dirty)" program. There are a number of techniques used in
> your code that are great in a "quick 'n' dirty" program such as this
> but would be bad in a real one. Since you say you want to learn Perl
> I'll point them out.
>
> Even in a quick program you should incude the error in your error
> messages!
>
> open(FH, "+<server_watch") || die $!;
>
> In serious programs you'd probably want to include more information:
>
> open(FH, "+<server_watch") || die "Error opening server_watch: $!";
>
> Note, since this is a quick program I'd have not bothered with the
> explicit close().
>
> In your program you use implicitly declared global variables. For
> programs below a certain size this can be a saving. For larger
> programs in becomes a pain. People disagree about where the line at
> which it becomes a pain is drawn but most would agree that by the time
> you get to 100 lines and half a dozen subroutines you are well past
> it. I, personally, am a very inaccurate typist so I draw the line at
> ~10 lines.
>
> However, if you are writing small programs not as quick programs in
> their own right but as a exercises in learning how to write real
> programs you should get into the habit of putting "use strict;" at the
> top of even your shortest programs and always declaring all variables
> as lexically scoped (using my) in the smallest applicable scope
> (unless there's a reson to do otherwise).
>
> In the case of your program there's only one variable so:
>
> $count = <FH>;
>
> Would become
>
> my $count = <FH>;
>
> Putting "use strict" will disable the implicit global variable
> declaration, it also disables two other features that you will rarely
> need and you really don't want enabled until you've understood what
> they do. (For details RTFM).
>
> Bare filehandles, IMHO, are also a feature that's best restricted to
> "quick" programs.
>
> In larger programs you are better off using a lexical variable.
>
> open(my $FH, "+<server_watch") || die "Error opening server_watch:
> $!";
>
> IMNSHO, the 2-arg open() in which the mode and file paramters are
> concatenated is largely a legacy feature. If you are just starting
> Perl now you may as well go straight to the newer syntax.
>
> open(my $FH,'+<','server_watch') || die "Error opening server_watch:
> $!";
>
> In larger programs you should always use the more flexible "use
> warnings" rather than the "-w" switch. In "quick" programs where the
> few keystokes is significant then use the -w by all means.
>
> In real (as opposed to Q&D) programs you should import the SEEK_SET
> constant from the Fcntl module rather than assume it is 0. That said
> I don't think there are any platforms on which Perl is ever likely to
> be implemented on which it is anything else!
>
> Oh and you may also wish to see FAQ: What's wrong with always quoting
> "$vars"?
>
> > Also, if anyone knows that there are already written little scripts
> > like this.

>
> The point about Q&D programs is you write them quickly to do one
> thing, there would be no point publishing them.
>
> Perhaps you should be looking at early examples in a Perl tutorial.
>
> Be aware that there are many bad tutorials out there. Most (but not
> all) of the bad ones can be spotted by the following features:
>
> They don't "use strict" and "use warnings".
>
> They either don't declare variables using my() or
> don't do so in the smallest applicable scope.
>
> They use & on all subroutine calls.
>
> > I like to look at other people's codes...... to learn.

>
> If you really want to look at other people's first attempts you should
> consider looking in some of the Perl newsgroups that exist in this
> hierachy (as opposed to this one that does not). That way you not
> only see the code but also the critique. Just looking at other
> people's first attempt would be a case of people who don't know what
> they are doing pooling their ignorance. Of course, if you really want
> to pool ignorance about Perl there's a newsgroup specially for that
> too: alt.perl!


thank you for your words.

I will take your advise & I will show improvements next time I post.

thank you!!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net 2 07-29-2008 09:50 AM
When using System.IO.FileStream, I write 8 bytes, then seek to the start of the file, does the 8 bytes get flushed on seek and the buffer become a readbuffer at that point instead of being a write buffer? DR ASP .Net Building Controls 0 07-29-2008 01:37 AM
is better to open, write, close file than open, write, append, close? Iulian Ilea Javascript 1 12-21-2006 04:32 PM
ASP createtext file not working, even with WRITE access Joey Martin ASP General 0 03-15-2006 07:11 PM
Write number variable to text file... not working... help! klub Java 8 10-31-2005 10:10 AM



Advertisments