Problem with File Handling?

  • Thread starter shashibhushan.lanka
  • Start date
S

shashibhushan.lanka

Hi,

This is my first post to this group. Please help me out.

print "you are about to write a new file\n";
open FHOUT, ">/root/n1" or die "can't open a new file\n";
while(<>)
{
print FHOUT "$_ \n";
}
#Using ^C to exit the while loop.
#The problem is that it is not writing the contents that are read from
the standatd input to the specified file.
close(FHOUT);


The above program is not writing anything into /root/n1. I am pressing
^C(ctrl -C) to break the while loop( in order to stop input from
standard input).


Please help me.
 
G

George

Hi,

This is my first post to this group. Please help me out.

please read the FAQ.
print "you are about to write a new file\n";
open FHOUT, ">/root/n1" or die "can't open a new file\n";
while(<>)

I assume you called your perl program without parameters, therefore
"<>" it will read from STDIN (see perldoc perlopentut)

I also assume that you called your perl program without redirection of
STDIN, therefore it will read from your keyboard.
{
print FHOUT "$_ \n";
}
#Using ^C to exit the while loop.
#The problem is that it is not writing the contents that are read from
the standatd input to the specified file.
close(FHOUT);


The above program is not writing anything into /root/n1.

That's because you did not type anything into your keyboard. You need
to enter some data:

You can type in several lines into your keyboard, followed by a return
after each line, if you want to finish your input from keyboard, you
need to hit a certain control-key (that control key is different for
different operating systems, for my operating system, it is ctrl-Z, but
it might be a different control-key for your operrating system).
 
L

Lukas Mai

(e-mail address removed) schrob:
Hi, [snip]
The above program is not writing anything into /root/n1. I am pressing
^C(ctrl -C) to break the while loop( in order to stop input from
standard input).

^C doesn't stop input, it sends a signal (SIGINT). Your program doesn't
catch it, so it dies. If you want to stop input, you need to use ^D.

HTH, Lukas
 
T

Tad McClellan

while(<>)

I am pressing
^C(ctrl -C) to break the while loop


Don't do that.

( in order to stop input from
standard input).


Control-C does NOT mark the end of stdin.

Control-C kills the program.

Control-D marks the end of stdin (Control-Z on Windows), try that instead.
 
G

George

Tad said:
Why?

Does one of the FAQs apply to the OP's question?

No, it does not. And I apologise for my (brainless) remark.
I was not thinking when I wrote "please read the FAQ".

I still hope that the other comments in my first reply were helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top