473,395 Members | 1,530 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,395 software developers and data experts.

How to append file to the file

Hi,

I need to append a contents of the temp file to another output file, tried copy, but it doesn't work. I can't directly print into the output file as I need to check contents of temp first.
I also tried to work with arrays instead of temp file, but printing array into INFO file gives me line shifting.

Will appreciate help:
--------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. use File::Copy;
  2.  
  3. $filec="copy.txt";
  4. $temp="temp.txt";
  5. $open(INFO, ">>$filec");
  6. open(TEMP, ">$temp");
  7.  
Sep 4 '07 #1
18 3051
numberwhun
3,509 Expert Mod 2GB
Welcome to TSDN!!!

First, please know that I have edited your post to add code tags around your code. Please be sure and add them to your post, around your code, for any future code postings.

Next, as far as the file(s) you are dealing with, I understand that you want to combine them into one file. Are you worried about duplicates? Is that what you are wanting to check for? Also, what operating system are you working on?

Regards,

Jeff
Sep 4 '07 #2
Hi,

I don't care about dups, during the loop processing I'm collecting some info into temp, then after storing this info into INFO I'll clean it up.
I'm using Active Perl on W'XP.

Tx
Vic
Sep 4 '07 #3
numberwhun
3,509 Expert Mod 2GB
Well, if that is the case, why not do something like this:

Expand|Select|Wrap|Line Numbers
  1. ### ALWAYS use strict and use warnings ###
  2. use strict;
  3. use warnings;
  4.  
  5. my $filec="copy.txt";
  6. my $temp="temp.txt";
  7.  
  8. ### Open the files handles: INFO for appending to and TEMP for writing to ###
  9. open(INFO, ">>$filec");
  10. open(TEMP, "<$temp");
  11.  
  12.  
  13. ## Cycle through the open temp file, appending each line out to the INFO file##
  14. while(<TEMP>)
  15. {
  16.     print INFO ("$_");
  17. }
  18.  
  19.  
  20. ### Close the file handles opened earlier ###
  21. close(INFO);
  22. close(TEMP);
  23.  
You were on the right track, but I wouldn't use File::Copy, I would just do a print of the file handle while cycling through it using a while loop since the INFO file handle was opened with a append(>>).

Also, always use strict and use warnings. They will keep you out of trouble and in check in most cases. I also changed the redirect for the TEMP file handle to a left angle which reads from. The right angle opens for writing only.

Let us know if you have any other issues.

Regards,

Jeff
Sep 4 '07 #4
Thanks, Jeff !

If works fine but for static temp file, which is not my case.
I go thru loop and depending of number of IFs collecting data into temp, then after one more IF I need to print it into INFO. So I still need
Expand|Select|Wrap|Line Numbers
  1. open(TEMP, ">$temp");
Tx
Vic
Sep 4 '07 #5
numberwhun
3,509 Expert Mod 2GB
Awesome, glad it worked for you! Yeah, just modify it as you need to. All I did was use the original code that you posted and made modifications/additions to it. Happy Perl scripting!

Regards,

Jeff
Sep 4 '07 #6
Sorry, Jeff

But it does NOT work in my case, I can't process temp if it's opened as output with > as you suggested below, I have zero output, though no errror.
It should work if I'll add close and open as <.


Tx
V
Sep 4 '07 #7
numberwhun
3,509 Expert Mod 2GB
If you look at the code I posted, you will see that I used the following:

Expand|Select|Wrap|Line Numbers
  1. open(TEMP, "<$temp");
  2.  
which will open the file for reading only. You should only use a > if you are only wanting to write to a file without reading from it.

Regards,

Jeff
Sep 4 '07 #8
KevinADC
4,059 Expert 2GB
when you open a file using '>', any existing content is lost. It essentially is creating a brand new file.
Sep 4 '07 #9
It's still confusing for most of guys:

I'm going thru the loop and depending of number of IFs collecting data into temp, then after one more IF I need to print it into INFO. So the question is how can do it in one shot in the very same script: put contents of TEMP file (that was dynamically created) into result INFO file, that opened in >> mode.


Tx
V
Sep 4 '07 #10
numberwhun
3,509 Expert Mod 2GB
Since we cannot see your code, why not post it here and we can try and help you get it to work. Without it, we can only speculate as to what you are doing.

Regards,

Jeff
Sep 4 '07 #11
KevinADC
4,059 Expert 2GB
It's still confusing for most of guys:

I'm going thru the loop and depending of number of IFs collecting data into temp, then after one more IF I need to print it into INFO. So the question is how can do it in one shot in the very same script: put contents of TEMP file (that was dynamically created) into result INFO file, that opened in >> mode.


Tx
V
Sounds like you are going about it OK. Nothing wrong with using a temp file or temp variables, sometimes it can not be avoided. But without seeing your code and understanding what you are doing there is no way to advise you on how to do it in "one shot". My guess is you could just write the data directly to INFO instead of TEMP.
Sep 4 '07 #12
Kelicula
176 Expert 100+
Also there is a read and write opener.
Expand|Select|Wrap|Line Numbers
  1. +<
as in:

Expand|Select|Wrap|Line Numbers
  1. open(INFO, "+<thefile.txt") or die
  2.  
Of course then you have to worry about position of writing in the file. (Were the write occurs)

Once again I'm sure it would help if you're code was visible.
Sep 5 '07 #13
numberwhun
3,509 Expert Mod 2GB
Actually, permissions really don't come into play since the OP is on a Windows XP machine.

Regards,

Jeff
Sep 5 '07 #14
KevinADC
4,059 Expert 2GB
permissions != position ;)
Sep 5 '07 #15
numberwhun
3,509 Expert Mod 2GB
Still, if the drive is mapped, then position wouldn't matter as access would already be established. Do you agree?
Sep 5 '07 #16
KevinADC
4,059 Expert 2GB
I believe he means position in the file as in "seek", if that is the case then the position could matter.
Sep 5 '07 #17
Tx to all,

I'm OK now, I opened TEMP file in > mode, then closed it, then opened as <, this way I can fill it and later read it into the final output INFO file. Thought that you an do it in simpler way, this way I got and extra loop,

Expand|Select|Wrap|Line Numbers
  1. open(INFO, ">>$filec");
  2. open(TEMP, ">$temp");
  3.  
  4. print TEMP "$one\n";
  5. print TEMP "$two\n";
  6. print TEMP "$three\n";
  7.  
  8. close(TEMP);
  9. open(TEMP, "<$temp");
  10. $cntr = 0;
  11. while (<TEMP>) {
  12.     $cntr=$cntr + 1;
  13.     print "counter = $cntr\n";  
  14.     print INFO ("$_");
  15. }
  16.  
  17. if ($cntr > 1) {
  18.     print "value of cntr=$cntr\n";
  19. }
  20.  
  21. print INFO " \n";
  22.  
  23.  
  24. print "Time   $tt\n";
  25.  
  26. close(INFO);
  27. close(TEMP);
Sep 5 '07 #18
numberwhun
3,509 Expert Mod 2GB
Just an additional note. When creating file handles, its always best to give them names that are most meaningful, causing less confusion on your part later or on the part of those who will have to support your code.

For instance, when opening the temp file for writing, you could use "TEMP_OUT" and when opening the temp file for reading, you could use "TEMP_IN". Just a suggestion, that's all, but albeit a handy one if you ask me.

Regards,

Jeff
Sep 5 '07 #19

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

Similar topics

2
by: Nico Grubert | last post by:
Hi there, I would like to open an existing file that contains some lines of text in order to append a new line at the end of the content. My first try was: >>> f = open('/tmp/myfile', 'w')...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Matrix | last post by:
Greetings I want to append records in a XML file,I use XMLTextWriter,but i found it only create a XML file,not append records in exist XML file.why?...
4
by: Amy L. | last post by:
It is my understanding that in order to append XML to a file you would have to read the file in as an XML document and insert the node. However, what if the file is close to 1GB in size? Would you...
14
by: vbMark | last post by:
Greetings, This seems like it should be simple but I can't figure out how to do this. I just want to append binary file 2 on to the end of binary file 1. Sample code please? Thanks!
5
by: Simone M | last post by:
Here is my problem: I need to append a string to a file every time the user downloads this file. The string is different for every user. I would like to append the string to the file via ftp. But...
3
by: Mohan | last post by:
Hi I face a problem with the stl string append on our 32-bit SLES9 machine . (It is not happening on SLES9 64-bit machines and on SuSE 10). When we read a 179MB file into the memory,...
2
by: Kevien Lee | last post by:
Hi , I had a strang problam ,when i use StreamWriter to append to a file,i found that if i don't close the StreamReader it couldn't write the data into file,the code as folllow that: class...
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
2
by: Curious | last post by:
I want to write to an output file, "C:\\temp\\debug.txt": If the file exists, append the new content to the end of the file (instead of overwriting the current content). Therefore, I want to...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.