Solved

Opening a file for write problem

Posted on 2002-03-28
13
155 Views
Last Modified: 2010-05-18
Hi,

I'd be grateful if anybody could help me with this problem as it's driving me mad!

Basically I have a flat text file called days.txt, in it are 7 lines, each corresponding to a day of the week seperated with the | symbol and a number:

Mon|0
Tue|1
Wed|1
Thu|0

etc...etc

I want to open the file, modify one of the numbers (dependant on some processing) then write the file away again.

My problem:

If I OPEN the file for WRITE, assign the contents to an array, then modify one line element then PRINT and CLOSE the file, I end up with a blank file other than one element (all existing contents are removed).

If I OPEN the file for APPEND, do the same, I get the amended line at the end of a blank file (regardless of what element I tell it to update).

And finally (obviously) if I OPEN the file READ ONLY, I can't write to it any way.

How do I open the file, read contents into the array, modify one array element, then write the file back keeping all the original data except the changed element?

Any help would be appreciated, although I probably won't check here until after Easter (Tuesday 02/04/02).

Regards,

Neil
0
Comment
Question by:NTIVER
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +3
13 Comments
 
LVL 1

Expert Comment

by:bluprint
ID: 6903221
Open the file for write, read the WHOLE FILE into memory, make your change (you might need to write a function to search for the day that corresponds to the number you want to modify, and return the position of the number), then write the whole array to the file.

0
 
LVL 16

Expert Comment

by:maneshr
ID: 6903673
bluprint,

Thank you for posting your comment.

As a new member of the EE community, Welcome!!

While posting a comment, it best not to propose it as an answer, unless you are 100% certain that this is the solution the user is looking for.

Even then most people prefer not to lock the question by selecting the "Answer" button, for the 2 main reasons.

1) Locking questions denys other users from seeing the question in the main area and therefore deprives the user of more alternatives.
2) Since we all learn more by sharing, we deny ourselves a chance to learn and increase our knowledge horizons.

Therefore, i would humbly request you to take back your answer and open up the question to all users.

Thanks in advance,

maneshr
(NOT a moderator at EE)
0
 
LVL 84

Expert Comment

by:ozo
ID: 6904276
{local $^I=".bak"; local @ARGV=("filename");
  while( <> ){
    s/(Tue\|)(\d+)/$1.($2+1)/e;
    print;  
  }
}
 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:Computer101
ID: 6905861
For reasons stated above, proposed answer rejected.

Computer101
E-E Moderator
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6905962
Computer101,

Thanks,
0
 
LVL 1

Author Comment

by:NTIVER
ID: 6912624
ozo - your solution looks very complicated, although judging from your massive 202991 points in the Perl topic area, I can probably safely assume that you know what you're talking about :-)

As I am quite new to the Perl language, could you just explain a little bit more about what your suggestion is doing?  I'm OK with variables and arrays but am finding it difficult to follow your solution through.

Could you just guide me through the parts please?

Many thanks in advance for your help.

Neil.
0
 
LVL 16

Expert Comment

by:maneshr
ID: 6913457
NTIVER,

Please post the final solution that you use here.

This will help us all know the answer that you were looking for.

Thanks in advance,
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 6914143
$^I is the inplace-edit extension.  Setting it is like running perl with the -i switch.
from `perldoc perlrun` the -i switch
                      specifies  that  files  processed by the <> construct
                      are to be edited in-place.  It does this by  renaming
                      the  input  file,  opening  the  output  file  by the
                      original name, and selecting that output file as  the
                      default  for  print()  statements.  The extension, if
                      supplied, is used to modify the name of the old  file
                      to make a backup copy, following these rules:
                      If  no  extension  is supplied, no backup is made and
                      the current file is overwritten.
You didn't say which element you wanted to modify, or how you wanted to modify it,
so as an example
s/(Tue\|)(\d+)/$1.($2+1)/e; #changes the line "Tue|1" to "Tue|2"
0
 
LVL 4

Expert Comment

by:LionBSD
ID: 6914464
you have to open a file for read close it and then open it again to write it..
i suggest you open the file to read keep the number you wanna change in a scalar and close the file
than you open the file agai for write and than you use substitute.. example (s/$1/$2)
for the one you wanna change with the number you kept before..you can post the script i'll fix it to you
0
 
LVL 1

Author Comment

by:NTIVER
ID: 6917801
ozo - I get it now, thanks for the explanation.

Let me have a play with your suggestion(s) and I'll get back and sort the points out (assuming it works <bg>) as soon as I can.

brb

Neil
0
 
LVL 1

Author Comment

by:NTIVER
ID: 6951126
ozo - you're suggestion works for the purposes of what I need - thank you very much, sorry it took a while to get back to you.

Neil
0
 
LVL 1

Author Comment

by:NTIVER
ID: 6951134
ozo - you're suggestion works for the purposes of what I need - thank you very much, sorry it took a while to get back to you.

Neil
0
 
LVL 1

Author Comment

by:NTIVER
ID: 6951136
LioBSD - thank you for your comments also - I've used your suggestion in a new script I'm developing which has other requirements.

Thank you everyone else ;)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 105
Need help with bash and/or perl commands on OS X Terminal 9 111
rename outfile before writing 2 76
ppm conversion to curl on a module install 8 89
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question