473,545 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
+ 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 2154
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
1705
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 doesn't exist. Right now, if it doesn't exist it just gives a error "page not found" in the frame I'm trying to open it in. Here is my code. ...
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, FileAccess.ReadWrite, FileShare.None); which does not prevent me from opening the file even if another user has the file open ie Notepad, it will prevent me...
0
1621
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 web address like window.open(http://.........). I have been asked to find out if it is possible to do something similar but instead opening a file...
1
1555
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 shouldn't have to manually change the file names first. char hand; cout<<"enter file name: "; cin>>hand; ifstream myfile (hand);
1
1249
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 various log files would be a good example of something like this. I think I've got the whole reading the text out of the file figured out, but just...
2
1977
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 around and played with a shell command but all that did is open my home page not the .url i was actually clicking on, any ideas?? any help is much...
5
2411
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 times that word occurs. Now I have this directory containing about 60 text files which I need to run this script on. Seeing as I'm not really a star in...
1
1072
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, it allows them to edit the document before closing it. The document type can be anything from Word to pdf. Currently I am using the following...
0
1048
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 running into is that when I send the path of the file to the system() command, which for those of you who don't know the system command is the...
34
5306
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 "<pyshell#10>", line 1, in <module> os.startfile("C:\Documents and Settings\Alex\My Documents\My
0
7486
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7676
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7932
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6001
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5347
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3456
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1905
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 we have to send another system
1
1032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
729
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.