• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 162
  • Last Modified:

Opening a file for write problem

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
NTIVER
Asked:
NTIVER
  • 5
  • 3
  • 2
  • +3
1 Solution
 
bluprintCommented:
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
 
maneshrCommented:
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
 
ozoCommented:
{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!

 
Computer101Commented:
For reasons stated above, proposed answer rejected.

Computer101
E-E Moderator
0
 
maneshrCommented:
Computer101,

Thanks,
0
 
NTIVERAuthor Commented:
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
 
maneshrCommented:
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
 
ozoCommented:
$^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
 
LionBSDCommented:
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
 
NTIVERAuthor Commented:
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
 
NTIVERAuthor Commented:
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
 
NTIVERAuthor Commented:
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
 
NTIVERAuthor Commented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now