473,465 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writing to file

11 New Member
Hi,
I have almost got my script to do what I want it to do, but I have one final problem for which I have been searching for a solution. My code now successfully reads the content of every text file in the directory, and in the correct order. I have also managed to exclude the lines I didn't want. What I need to do now is to write this very long list of text to a new file. I have tried this, but what it seems to be doing is writing a line to it, removing it and writing another line to it, and repeats this until it reaches the end.

This is the code I am using at the moment:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. opendir(DIR, "directory") || die "Cannot open directory";
  4. my @file = readdir(DIR);
  5. closedir(DIR);
  6.  
  7. foreach my $file (@file)
  8. {
  9.     @file = sort(@file);
  10.     open(FH, $file) or die "$!";
  11.  
  12.     while (<FH>)
  13.     {
  14.         if ((/0000\s/) || (/0004\s/) || (/0100\s/) || (/0200\s/))
  15.     {
  16.             chomp;
  17.         open(NF, ">file" || die "Cannot open file");
  18.             print NF "$_\n";
  19.         close(NF);
  20.     }
  21.     }
  22. }
  23. close(FH);
  24.  
I would like this code to write every single line that is read in the command prompt into a new file. Any help would be appreciated, thanks in advance.
Oct 5 '07 #1
5 1635
mehj123
55 New Member
You should open the file in append mode
Expand|Select|Wrap|Line Numbers
  1.  open(NF, ">>$file" || die "Cannot open file");
and you have written "file" in place of "$file"
Oct 5 '07 #2
dh87lfc
11 New Member
You should open the file in append mode
Expand|Select|Wrap|Line Numbers
  1.  open(NF, ">>$file" || die "Cannot open file");
and you have written "file" in place of "$file"
Ok, thanks, I will try that. I only put "file" in to save having the full file address and name.
Oct 5 '07 #3
dh87lfc
11 New Member
I tried this and I had limited success. It wrote in every single line, but because the new file itself now has the 0000, 0004, 0100 and 0200 that I declared it must have, it is now writing the text in over and over again. I think what I need to do is to exclude the new filename so that it will ignore these numbers in this file. Is that right?

Thanks
Oct 5 '07 #4
mehj123
55 New Member
Ya i think so.. you have to consider the files from the directory only and not the new file right.. so i guess excluding the new file will work...please try and let me know too :)
Oct 5 '07 #5
dh87lfc
11 New Member
Ya i think so.. you have to consider the files from the directory only and not the new file right.. so i guess excluding the new file will work...please try and let me know too :)
Hi again,
I figured out the problem. I have opened the new output file within the loop, which is why it is constantly writing the data in over and over again. Bit of a schoolboy error. I moved the line that opens the new file to immediately after where I closed the directory, and also moved the line that closes the new file to the bottom, so none of it is inside the foreach loop. This has solved the problem, it only reads the data in once now as desired.
Oct 5 '07 #6

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.