Problem with File Handling?

Discussion in 'Perl Misc' started by shashibhushan.lanka@gmail.com, Mar 20, 2006.

  1. Guest

    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.
     
    , Mar 20, 2006
    #1
    1. Advertising

  2. George Guest

    wrote:
    > 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).

    > I am pressing
    > ^C(ctrl -C) to break the while loop( in order to stop input from
    > standard input).
     
    George, Mar 20, 2006
    #2
    1. Advertising

  3. Lukas Mai Guest

    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
     
    Lukas Mai, Mar 20, 2006
    #3
  4. <> wrote:

    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 20, 2006
    #4
  5. George <> wrote:
    > wrote:
    >> Hi,
    >>
    >> This is my first post to this group. Please help me out.

    >
    > please read the FAQ.



    Why?

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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Mar 20, 2006
    #5
  6. George Guest

    Tad McClellan wrote:
    > George <> wrote:
    > > wrote:
    > >> Hi,
    > >>
    > >> This is my first post to this group. Please help me out.

    > >
    > > please read the FAQ.

    >
    >
    > 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.
     
    George, Mar 21, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. uwb
    Replies:
    4
    Views:
    368
  2. Rajen
    Replies:
    21
    Views:
    581
    Dave Thompson
    Sep 21, 2006
  3. Mark Tarver
    Replies:
    22
    Views:
    1,337
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,976
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    202
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page