473,399 Members | 3,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

write to file not working

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
Jul 19 '05 #1
6 3814
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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.
Jul 19 '05 #2
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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");
Jul 19 '05 #3
mr*****@webbox.com (Jeff Dunn) wrote in message news:<5b**************************@posting.google. com>...
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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.
Jul 19 '05 #4
mr*****@webbox.com (Jeff Dunn) wrote in message news:<5b**************************@posting.google. com>...
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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.
Jul 19 '05 #5
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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!
Jul 19 '05 #6
no****@mail.com wrote in message news:<4d**************************@posting.google. com>...
rx****@hehe.com wrote in message news:<8a**************************@posting.google. 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!!
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
0
by: chipotle_pickle | last post by:
You're letting a lot of bad links creep onto that page. Do a Google search for "genesis world energy". The second item found is a broken link to the newsroom. (The third item found explains how the...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
7
by: vinthan | last post by:
hi, I am new to python. I have to write test cases in python. An application is open in the desk top ( application writen in .Net) I have to write code to get focuse the application and click on...
2
kamill
by: kamill | last post by:
i need to write content of one text file into another text file. My code is working ,if i choose both files from same directory where my program reside..BUT,its not working if i select files from...
1
by: sandhyamn | last post by:
hi...i am trying to write a programme for uploading file with jsp using multipart/form data.it's working.....but the problem is i can't write the textbox values into a file....if change the form...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
2
by: raghunadhs | last post by:
Hi All, i have some simple scripts( which may have some insert statements, update statements etc). these scripts were written in Sql server 2000. now i want to use the same scripts in Access. (since...
1
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.