473,480 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Modes for opening files !?

43 New Member
Hello guys,

I have not figure out how to properly use the read/write mode for opening files +>, +<, +>>

I tried it but always got unpredicatable output.

I usually do this for standard read/write operation:

Expand|Select|Wrap|Line Numbers
  1. open(NEW_DATABASE, ">$file2");
  2. open(DATABASE, "<$file1");
  3.  
  4. while (<DATABASE>) {
  5.     $line = $_;
  6.     chop $line;
  7.  
  8.     # Some operations on the line
  9.  
  10.     print NEW_DATABASE  "line\n";
  11. } # end of while (<DATABASE>)
  12.  
  13. close(DATABASE);
  14. close(NEW_DATABASE);
  15.  
  16. rename($file2, $file1);
Which work fine

Then I tried this code bellow. Not really to save 3 lines of code but to understand how to use the R/W mode...

Expand|Select|Wrap|Line Numbers
  1. open(DATABASE, "+>$file1");  
  2.  
  3. while (<DATABASE>) {
  4.     $line = $_;
  5.     chop $line;
  6.  
  7.     # Some operations on the line
  8.  
  9.     print  DATABASE  "line\n";
  10. } # end of while (<DATABASE>)
  11.  
  12. close(DATABASE);
The output was very unpredicatable. It did not do what I wanted.

Is there a way to use these read/write mode to rewrite a single line in one pass or should I stick to the first design, which work?

Thanks,

Yves

PS: I did not forgot the tag this time ;-)
Feb 18 '11 #1
4 2151
numberwhun
3,509 Recognized Expert Moderator Specialist
Ha ha, I noticed. Thank you! :)

For a good understanding of this topic, you may want to check out this tutorial. Hopefully it will help with your understanding.

Regards,

Jeff
Feb 18 '11 #2
miller
1,089 Recognized Expert Top Contributor
Well, to start off with I have a few thoughts/suggestions concerning your first method of translating the file:
  1. use strict;
  2. Use Lexical filehandles in three part form.
  3. Add error checking to your file open operations
  4. use chomp over chop since you're only wanting to remove return characters
  5. chomp first before assigning to a variable, just makes things simpler

Anyway, to test out the code I created the following data file called foo.dat

Expand|Select|Wrap|Line Numbers
  1. This is just testing if we can translate some numbers.
  2.  
  3. It will taking any digits like 1 2 3 and perform some
  4. operation on them.  The whole idea is that we are 
  5. just able to test out modes of reading and writing.
  6. It will ideally be a lossless translation that is also
  7. it's own inverse.
  8.  
  9. More numbers like 45 and 79 for no particular reason.
  10.  
I then came up with a translation we could do on the file. I decided on taking any digit and adding 5 to it and then modulus 10. This way all numbers will be changed back to their original value by doing the translation a second time.

Expand|Select|Wrap|Line Numbers
  1. use File::Copy qw(move);
  2.  
  3. use strict;
  4.  
  5. my $file = 'foo.dat';
  6.  
  7. my $newfile = $file . '.new';
  8.  
  9. open my $oh, '>', $newfile or die "Can't open file, $newfile: $!";
  10. open my $ih, $file or die "Can't open file, $file, $!";
  11.  
  12. while (<$ih>) {
  13.     chomp;
  14.     my $line = $_;
  15.  
  16.     #       Some operations on the line
  17.     $line =~ s/(\d)/($1 + 5) % 10/eg;
  18.  
  19.     print $oh "$line\n";
  20. }      # end of while    while (<$ih>)
  21.  
  22. close $oh;
  23. close $ih;
  24.  
  25. move($newfile, $file) or die "Can't move $newfile -> $file: $!";
  26.  
  27. 1;
  28.  
  29. __END__
Finally, the whole point of this question was that you were curious about read/write mode for open. I'd actually discourage you from using that as it's only really useful for binary files. If you want to do read/write operations on a text file then check out Tie::File, as it will do all the hard work for you.

Expand|Select|Wrap|Line Numbers
  1. use Tie::File;
  2.  
  3. use strict;
  4.  
  5. my $file = 'foo.dat';
  6.  
  7. tie my @data, "Tie::File", $file, autochomp => 0
  8.     or die "Can't open file, $file: $!";
  9.  
  10. for (@data) {
  11.     #       Some operations on the line
  12.     s{(\d)}{($1 + 5) % 10}eg;
  13. }
  14.  
  15. untie @data;
  16.  
  17. 1;
  18.  
  19. __END__
- Miller
Feb 18 '11 #3
RonB
589 Recognized Expert Moderator Contributor
Expand|Select|Wrap|Line Numbers
  1. while (<$ih>) {
  2.     chomp;
  3.     my $line = $_;
  4.  
Is better written as:
Expand|Select|Wrap|Line Numbers
  1. while ( my $line = <$ih> ) {
  2.     chomp $line;
  3.  

And I'd also add the warnings pragma.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
Feb 18 '11 #4
yjulien
43 New Member
Wooooow !

I think that I will stick with my original method, which is quite simple after all… and works :D

There is an American "say" I love and use all the time. It goes "if it ain't broken, don't fix it!"

Thanks guys,

Yves
Feb 18 '11 #5

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

Similar topics

1
1703
by: Jonathan Rubke | last post by:
I am getting a filename from one frame and opening it in another frame. It works fine if the file exists, but I'm trying to get it to show an error message, possibly an alert box, if the file...
5
714
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
0
1599
by: Stephen | last post by:
could someone give me some advise on a problem I have. At present i have a web application which displays pdf files in a web browser using javascript and the window.open method and pointing to a...
1
1551
by: arganx | last post by:
The following does not work when opening a file that contains a space in the name. It works perfectly on files that have no space in the name. Any ideas on how to make this work? Surely I...
1
1239
by: Ddraig | last post by:
Howdy, I was wondering if anyone could point me in the direction of some good code that will look at a folder of various text files, read in the names and then open them. For example opening...
2
1974
by: Asprisa | last post by:
Hi There, This is probably an easy one! i am having trouble opening the files listed in a filelistbox, they are all .url files, all i want them to do is open up in the default browser, i looked...
5
2406
by: muwie | last post by:
Hello, I was browsing to see if I could find something similair to my problem. But I couldn't find anything.. I have this script that counts every word in a file. And then also says how many...
1
1069
by: Jonno85 | last post by:
Hi, I have developed a file directory to show files on one of our servers, but am experiencing problems when it comes to opening and saving files. I would like it so that when a user opens a file,...
0
1042
by: Alexnb | last post by:
Okay, so what I want my program to do it open a file, a music file in specific, and for this we will say it is an .mp3. Well, I am using the system() command from the os class. The problem I am...
34
5290
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
6903
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
7071
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...
1
6726
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
5318
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,...
1
4763
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
4468
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2987
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...
0
1291
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
170
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.