Appending to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • homesick123
    New Member
    • Oct 2006
    • 8

    Appending to a file

    Hi
    I am appending the data to a file . I want this to happen from a newline everytime the script is run.For eg.
    the file1.txt contains

    A quickcfsgggdra ertge
    Brown fox s;ofj;ekf;okfo
    jumped sldkfhlrigje

    Now I want to append the follwoing data from file2.txt

    over the lazy dog

    now file1.txt should look like

    A quickcfsgggdra ertge
    Brown fox s;ofj;ekf;okfo
    jumped sldkfhlrigje
    over the lazy dog

    Please suggest the code!
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2


    Simply open the file handle in append mode, and be sure that each line that you print ends in a new line character.

    Code:
    open(OUT, ">> file1.txt") or die "open $file1.txt failed: $!";
    print OUT "Your next line of data\n";
    close OUT;
    Also, browse the faq on files provided by perldoc at some point. And at the very least keep it bookmarked if you want to quick answers to a lot of the comment file questions:

    Comment

    • docsnyder
      New Member
      • Dec 2006
      • 88

      #3
      @homesick123

      The 3-arg form of open() should be preferred:
      Code:
      open(OUT, ">>", "file1.txt") or die "open $file1.txt failed: $!";
      instead of
      Code:
      open(OUT, ">> file1.txt") or die "open $file1.txt failed: $!";
      Greetz, Doc

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        The 3-arg form of open() should be preferred:
        Why is that preferred?

        Not even the examples on the perdoc site use the 3 argument form:

        Code:
         open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
        	    or die "Can't open mailbox: $!";

        Comment

        • docsnyder
          New Member
          • Dec 2006
          • 88

          #5
          Originally posted by KevinADC
          Why is that preferred?

          Not even the examples on the perdoc site use the 3 argument form:

          Code:
           open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
          	    or die "Can't open mailbox: $!";
          Dear Kevin!

          Study this "http://perldoc.perl.or g/functions/open.html" carefully and you will know it.

          Greetz, Doc

          Comment

          • miller
            Recognized Expert Top Contributor
            • Oct 2006
            • 1086

            #6
            To docsnyder,

            It is not preferred. It is simply preferred by "you".

            If you want to debate your reasoning, feel free. But I can assure you that Kevin has read the docs before many times, as have I.

            Comment

            • docsnyder
              New Member
              • Dec 2006
              • 88

              #7
              Originally posted by miller
              To docsnyder,

              It is not preferred. It is simply preferred by "you".

              If you want to debate your reasoning, feel free. But I can assure you that Kevin has read the docs before many times, as have I.
              Dear miller!

              If you have read my posting carefully, you should have noticed, that my way of "direct" help has been a benefit to a lot of peoples anywhere in the world (do you really request a list of positive answers right now? Believe me, there is one.). But in my eyes, the help that is given in "thescripts " is in a "VERY" poor shape! In my preferred forum (http://board.perl-community.de (sorry, but it's german)), people help each other in a way, I never have encountered anywhere else. Even if total novice asks a question, and he/she will get an answer. In "thescripts " I noticed the fact, that seldomly anyone is trying to answere a question, but stating one more question is more frequently the case.

              Miller et all: If you really have read the reference (let's assume Kevin have done it!) I pointed out (see my last mail): I can hardly understand, why you oppose to my aim/conviction!

              Greetz, Doc

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Dear Doc!

                As I suspected (well, already knew) there is no mention of the three argument syntax being prefered. It is simply a different way of doing something that can be handy in certain situations.

                Comment

                • docsnyder
                  New Member
                  • Dec 2006
                  • 88

                  #9
                  Originally posted by KevinADC
                  Dear Doc!

                  As I suspected (well, already knew) there is no mention of the three argument syntax being prefered. It is simply a different way of doing something that can be handy in certain situations.
                  O.K. Kevin, use the 2-argument syntax if you feel lucky with it. If the arguments of the cited article did not convince you, I can life with it. I prefere to life another style of (programming-)life.

                  Greetz, Doc

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    There you go, it's your preference, not "the" prefered way to open files with perl. The documentation in the link you posted (and in the opentut) makes that very clear. Thats all for me on this subject.

                    Comment

                    • docsnyder
                      New Member
                      • Dec 2006
                      • 88

                      #11
                      @Kevin
                      Originally posted by KevinADC
                      There you go, it's your preference, not "the" prefered way to open files with perl. The documentation in the link you posted (and in the opentut) makes that very clear. Thats all for me on this subject.
                      Have you ever thought about security issues? With the "2 arg" form of open(), which typically appears like
                      Code:
                      open(FD, $file);
                      it may happen, that $file gets a value which is far away from what you intended to to. Think for example, $file is set to ARGV[0]. A horror vision!!! Someone could execute arbitrary code! Did you take this into account?

                      The described risk of exploitation is not given with the "3 arg" form of open().

                      Greetz, Doc

                      P.S. If it turns out that to be secure is only "my own personal preferrence", O.K., I can live with it.

                      Comment

                      • KevinADC
                        Recognized Expert Specialist
                        • Jan 2007
                        • 4092

                        #12
                        Originally posted by docsnyder
                        @Kevin

                        Have you ever thought about security issues? With the "2 arg" form of open(), which typically appears like
                        Code:
                        open(FD, $file);
                        it may happen, that $file gets a value which is far away from what you intended to to. Think for example, $file is set to ARGV[0]. A horror vision!!! Someone could execute arbitrary code! Did you take this into account?

                        The described risk of exploitation is not given with the "3 arg" form of open().

                        Greetz, Doc

                        P.S. If it turns out that to be secure is only "my own personal preferrence", O.K., I can live with it.

                        No, in ten+ years of coding perl I have never thought about security issues.

                        Have you never heard of validating or untainting data before it's used as a filehandle or for any other insecure purpose?

                        Does being a condescendng **** come natural to you or does it take effort? ;)
                        Last edited by MMcCarthy; Feb 21 '07, 08:57 PM. Reason: editing inappropriate language

                        Comment

                        • MMcCarthy
                          Recognized Expert MVP
                          • Aug 2006
                          • 14387

                          #13
                          Ok guys,

                          You have got to stop arguing like this in threads. The OP doesn't need to be entertained in this manner. Kevin, use of profane language of any type is strictly prohibited on this site and repeated use can lead to a temporary site ban so please be more careful.

                          You both have the right to express different opinions and it's up to the OP which opinion they take but please do so without attacking each other. If you have a difference of opinion and want to argue a point then please do so using the PM system and in a polite and respectful manner.

                          The issue here is not who is right in this argument but the fact that you are essentially hijacking the original posters thread for your own argument. If I were the original poster I would be afraid to reply with any questions for fear of being drawn into the argument. This has got to stop.

                          If you wish to comment any further on this matter with me please contact me by PM.

                          Mary

                          Comment

                          • docsnyder
                            New Member
                            • Dec 2006
                            • 88

                            #14
                            Originally posted by KevinADC
                            No, in ten+ years of coding perl I have never thought about security issues.

                            Have you never heard of validating or untainting data before it's used as a filehandle or for any other insecure purpose?

                            Does being a condescendng **** come natural to you or does it take effort? ;)
                            Tainted data is only complained by Perl when executing binaries with the set-uid-bit enabled for binaries owned by root. Since a lot of sensible files are at least readable anyhow, someone could exploit sensible data this way, even beeing an ordinary user, with no "tainted data mechanism" in affect.

                            If you never ran into trouble about this issue since ten years, be happy that you obviousely work in an area in which security does not seem to be an issue. As I am agitating within the infrastructure of a bank, this is surely an issue, take this for granted. The revision department would fire me immediately if I would be inattentive about this matter.

                            But if this does not apply for you, you don't have to bother, you are absolutely right. Don't care.

                            Greetz, Doc

                            Comment

                            • KevinADC
                              Recognized Expert Specialist
                              • Jan 2007
                              • 4092

                              #15
                              apology to homesick123, my bad mate.

                              Comment

                              Working...