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

File write problem

23
Normally I'm okay writing files, but this is something I have never tried before and am not getting the correct result in the new file.

A file contains some text with a header. I can grab the header okay. What I want to do is write the entire rest of the file to a new one.

The problem is, it only writes the last line to the new file.

Text File (real one would contain a lot more)
Expand|Select|Wrap|Line Numbers
  1. Aaaaaa
  2. aaaaaaaa aaaaaaaa aaaaaaaa
  3.  
  4. bbbb bbbb bbbb bbbb bbbb
  5.  
  6. dddddddddd dddddddddd
  7.  
The Code
Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
  4. print "Content-type: text/html\n\n";
  5. ## NOTE: for Perl 5.6 and earlier
  6.  
  7. my $txf="file.txt";
  8. my $opfile="opf.txt";
  9. # get first line
  10. open(TXF,"$txf"); my $title=<TXF>;  close TXF;
  11.  
  12. # Get rest and write to new file
  13. open(TXF,"$txf");
  14.  readline(TXF); # ignore first line
  15.  while(<TXF>) {
  16.   open(OPF,">$opfile");
  17.    print  $_, "<br>"; # good to screen
  18.  
  19.    #### --- need something here but do not know what ????
  20.  
  21.    print OPF $_; # only writes last line into file
  22.   close OPF;
  23.  }
  24. close TXF;
  25.  
  26. # Finish
  27. print qq~<h2>Done</h2>~;
  28. exit;
  29.  
Hoep someone has the answer. I don't want to use a module for something this small.

Thanks.
Nov 9 '16 #1

✓ answered by Roamer

Thanks Ron. I use that method for other stuff, but didn't try it here (my Duhh).

Works fine, thanks again.

2 4096
RonB
589 Expert Mod 512MB
There are a lot of problems with your code but the problem you're asking about is caused by opening the output filehandle inside the loop.

Take that open call out of the loop and put it up next to the other open call and put the close statement after the loop.
Nov 9 '16 #2
Roamer
23
Thanks Ron. I use that method for other stuff, but didn't try it here (my Duhh).

Works fine, thanks again.
Nov 9 '16 #3

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

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
0
by: Todd Matson | last post by:
I am having a file-locking problem with an Access database. The .mdb and ..ldb files both remain locked even after all users have closed Access. The project is an Access 2000 database split into...
4
by: phantom | last post by:
Hi All. I am having a problem writing to a file. I can successfully open the file and write the contents using fprintf, however if the writing is not done for a while in the process the file...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
2
by: MVanriper | last post by:
Just getting started with Visual C++. Learning how classes work. When I select the icon to "RUN" my program to debug. The program compiles and I get an error due to syntax. I see my problem there,...
5
by: Tim_Mac | last post by:
hi, i read that by adding the following code to by aspx pages, it would not store temporary internet files: Response.Cache.SetCacheability(HttpCacheability.NoCache); it didn't actually work...
4
by: welch | last post by:
while taking some rough disk performance measures on windows machines, and snooping with FileMon, i've noticed some odd behavior here's the little nul-writer i'm running: def writeTest(nBlocks,...
11
by: A.M | last post by:
Hi, I found print much more flexible that write method. Can I use print instead of file.write method? Thank you,
4
by: apriebe47 | last post by:
Alright, I realize this is probably very basic to be posted on this newsgroup but I cannot figure out what is causing my problem. Here is the code I am using below: from getpass import getpass ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.