473,406 Members | 2,769 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,406 software developers and data experts.

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!
Feb 19 '07 #1
14 3058
miller
1,089 Expert 1GB
http://perldoc.perl.org/functions/open.html

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

Expand|Select|Wrap|Line Numbers
  1. open(OUT, ">> file1.txt") or die "open $file1.txt failed: $!";
  2. print OUT "Your next line of data\n";
  3. close OUT;
  4.  
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:

http://perldoc.perl.org/perlfaq5.html
Feb 19 '07 #2
@homesick123

The 3-arg form of open() should be preferred:
Expand|Select|Wrap|Line Numbers
  1. open(OUT, ">>", "file1.txt") or die "open $file1.txt failed: $!";
instead of
Expand|Select|Wrap|Line Numbers
  1. open(OUT, ">> file1.txt") or die "open $file1.txt failed: $!";
Greetz, Doc
Feb 20 '07 #3
KevinADC
4,059 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1.  open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
  2.         or die "Can't open mailbox: $!";
  3.  
Feb 20 '07 #4
Why is that preferred?

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

Expand|Select|Wrap|Line Numbers
  1.  open(MBOX, ">>/usr/spool/mail/$ENV{'USER'}")
  2.         or die "Can't open mailbox: $!";
  3.  
Dear Kevin!

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

Greetz, Doc
Feb 20 '07 #5
miller
1,089 Expert 1GB
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.
Feb 20 '07 #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.
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
Feb 20 '07 #7
KevinADC
4,059 Expert 2GB
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.
Feb 20 '07 #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.
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
Feb 20 '07 #9
KevinADC
4,059 Expert 2GB
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.
Feb 21 '07 #10
@Kevin
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 21 '07 #11
KevinADC
4,059 Expert 2GB
@Kevin

Have you ever thought about security issues? With the "2 arg" form of open(), which typically appears like
Expand|Select|Wrap|Line Numbers
  1. 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? ;)
Feb 21 '07 #12
MMcCarthy
14,534 Expert Mod 8TB
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
Feb 21 '07 #13
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
Feb 21 '07 #14
KevinADC
4,059 Expert 2GB
apology to homesick123, my bad mate.
Feb 21 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Thomas Heller | last post by:
I want to append/insert additional data to an xml file. Context: I use gccxml to parse C header files. gccxml creates an xml file containing all the definitions from the header files. The xml...
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
7
by: Don | last post by:
Hi all, With regards to the following, how do I append the datetimestamp to the filenames in the form? The files are processed using the PHP script that follows below. Thanks in advance,...
1
by: Jonathan Taylor | last post by:
I have a large XML file, that is too large to read in to XmlDocument. I need to append data to this XML file without creating a new file, since I don't want to have two copies of the large file...
16
by: Michael | last post by:
I have a data application in a2k that I need to create two fixed width text files and then combine them to a single file The first file is header information and the second is transaction data. ...
2
by: tony.collings | last post by:
I started a thread here : http://groups.google.co.uk/group/microsoft.public.cmserver.general/browse_thread/thread/29d63077144004a9/c3888efdcb7338f6?hl=en#c3888efdcb7338f6 About creating an Email...
4
by: jasper | last post by:
How can this be done? Thanks
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
1
by: ofuuzo1 | last post by:
Hi, Is there anyway I can append a new element to an existing xml without first loading the existing file into a variable, adding the new element into the variable and saving it by overwriting the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
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.