Solved

Opening a file for write problem

Posted on 2002-03-28
13
152 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
  • 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now