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

Reading and Writing to File

Hi,

I'm currently writing a perl script and it's my first time doing this. So I'm not sure why I can't seem to write to the same file that I am reading. I read somewhere that I cannot read and write at the same time. Is this true? If not, can you help me in solving this writing problem I am having? Below is my code:

my $midtierGS=`ls mtb_*_*_*_hnw_gs.xml`;

my $disclaimer="DYNAMIC_DISCLAIMER_IMPLEMENTED";
my $provider="MKD_CURRENT_DATA_PROVIDER";

open(FILE,"+<$midtierGS") or die "Couldn't open file!";
#read line from file
while ($record = <FILE>) {
#if line contains value
if ($record =~ m/$disclaimer/) {
#go to next line
$nextLine = <FILE>;
#substitute old value for new value
$nextLine =~ s/false/true/;
#save changes to file
print FILE "$nextLine";
}
elsif ($record =~ m/$provider/) {
$nextLine = <FILE>;
$nextLine =~ s/RSF/Comstock/;
print FILE "$nextLine";
}
}
close FILE;
Oct 19 '06 #1
1 8444
miller
1,089 Expert 1GB
From Perl Cookbook Ver 1 Chap 7.10 - Modifying a File in place without a temporary file

The operating system treats files as unstructured streams of bytes. This makes it impossible to insert, modify, or change bits of the file in place. (Except for the special case of fixed-record-length files). You can use a temporary file o hold the changed output, or you can read the entire file into memory, change it, and write it back out again.

To that end I give you two files. One a script to make this translation and other a file with fake data. I left things as simple as possible for you instead of dealing with file locking, or even using perl shortcut variables.

fileop.pl
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $file = shift;
  4. my $tmp = $file . ".tmp";
  5.  
  6. my $disclaimer = "DYNAMIC_DISCLAIMER_IMPLEMENTED";
  7. my $provider = "MKD_CURRENT_DATA_PROVIDER";
  8.  
  9. open(OLD, "< $file") or die "open $file: $!";
  10. open(NEW, "> $tmp") or die "open $tmp: $!";
  11.  
  12. my %toggleBool = qw(true false false true);
  13. my %toggleComp = qw(RSF Comstock Comstock RSF);
  14.  
  15. while (my $line = <OLD>) {
  16.     print NEW $line or die "print $tmp: $!";
  17.  
  18.     if ($line =~ m/$disclaimer/) {
  19.         $line = <OLD>;
  20.         $line =~ s/(true|false)/$toggleBool{$1}/;
  21.         print NEW $line;
  22.     } elsif ($line =~ m/$provider/) {
  23.         $line = <OLD>;
  24.         $line =~ s/(RSF|Comstock)/$toggleComp{$1}/;
  25.         print NEW $line;
  26.     }
  27. }
  28. close(OLD) or die "close $file: $!";
  29. close(NEW) or die "close $tmp: $!";
  30.  
  31. unlink($file) or die "unlink $file: $!";
  32. rename($tmp, $file) or die "can't rename $tmp to $file: $!";
  33.  
  34. 1;
  35.  
  36. __END__
  37.  
fileop.txt
Expand|Select|Wrap|Line Numbers
  1. foo bar like text
  2. DYNAMIC_DISCLAIMER_IMPLEMENTED
  3. false
  4. barbaz text
  5. MKD_CURRENT_DATA_PROVIDER
  6. RSF plus some other data
  7. bar foo text
  8. qwerty
  9. asdf jkl
  10.  
Oct 25 '06 #2

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

Similar topics

4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
2
by: Jeevan | last post by:
Hi, I have an array of data (which I am getting from a socket connection). I am working on a program which acts on this data but the program is written to work on data from a file (not from an...
3
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
2
by: John Salerno | last post by:
I wrote this code just to experiment with writing to and reading from a file. It seems to work fine when writing, but when reading the file, it only prints the filepath to the screen, not the file...
7
by: utab | last post by:
Hi there, I am trying to read from a file and at the same time change certain fields of the same field, there are 6 fields in this file like 1 2 3 4 5 6...
8
by: smeenehan | last post by:
This is a bit of a peculiar problem. First off, this relates to Python Challenge #12, so if you are attempting those and have yet to finish #12, as there are potential spoilers here. I have five...
5
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I...
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;
0
by: yogesh_anand | last post by:
Hi I am using sun solaris 5.9 operating system.Sometimes after reading from and while writing to file process size used to get increase.(it can be after 20th times 40 th time) I doesn't happen...
42
by: psbasha | last post by:
Hi, Is it necessary in Python to close the File after reading or writing the data to file?.While refering to Python material ,I saw some where mentioning that no need to close the file.Correct me...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.