CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Change a file

 



Sunchey
Novice

Nov 29, 2012, 5:05 AM

Post #1 of 7 (2023 views)
Change a file Can't Post

Hello.

Don't understand work of script.


Code
#!/usr/bin/perl -w 
use strict;
use diagnostics;
if (!open FILE, "+<file.txt") { die "Error: $!\n" } # open file for read/write
#$^I = ".bak";
while (<FILE>) #while reading strings from file
{
s/^\t//g; # erase tabs at the beginning of file , s/^\s//g do the same
print FILE; # print write to STDOUT
}
close FILE;


Script working:
1) tabs has been erased but some symbols had been erased too (And random number of character, why?)
2) all string duplicated. Why? If I commented "print FILE;" string, file has not change but just s/// must change file, isn't it?.

File has text type, within string has bytes: 0x0A, 0x0D, sometimes 0x09. Encoding windows 1251.

Thank you.


Laurent_R
Veteran / Moderator

Nov 29, 2012, 10:18 AM

Post #2 of 7 (2008 views)
Re: [Sunchey] Change a file [In reply to] Can't Post

Hi,

Your should not try to write to the same file as the one you read from. In most cases, this is at best messy.

Open one file (with one filehandler) for reading and another file (with another filehandler) for writing. After the process is completed and your files closed, just rename them to do the house cleaning.

Tell us if you still have these problems once you've changed that.


Sunchey
Novice

Nov 29, 2012, 11:41 PM

Post #3 of 7 (2001 views)
Re: [Laurent_R] Change a file [In reply to] Can't Post

Yes, I understand for 2 files. But why some symbols had been erased too?
For other script the file changing without second file, only due to $`I = ".bak". But if I commented this string, the file don't changed.


Code
#!/usr/bin/perl -w 
use strict;
use diagnostics;
$^I = ".bak"; # if I commented this string, print go to STDOUT and file don't changed. Why?
while (<>)
{
s/^\t//g;
print;
}


Why it doing with 1 file? (And "$^I = ".bak";", but in first script it is not helped)


Laurent_R
Veteran / Moderator

Nov 30, 2012, 12:28 AM

Post #4 of 7 (1999 views)
Re: [Sunchey] Change a file [In reply to] Can't Post

 
As I said, if you try to write to the same file as the one you're reading from, you'll get unexpected results. I suspect that if you fix that, you will no longer be losing part of your input. That's why I told you to change that and see if you still have a problem. If you still do, you'll have to tell us more about your input data.

As for $^I = ".bak;", this variable is the $INPLACE_EDIT spedial variable, it enables you to modify a file that you are reading from (it really does exactly what I suggested you to do: open a file for reading, open another one for writing and doing the renaming of the files without you having to do it explicitely (the original file is renamed with the ".bak" extension).


Sunchey
Novice

Nov 30, 2012, 1:55 AM

Post #5 of 7 (1997 views)
Re: [Laurent_R] Change a file [In reply to] Can't Post

Ok, thank you for answers. Worked with two files.
But what for exists "+<" and "+>"? Isn't it read/write mode for opened file?


Laurent_R
Veteran / Moderator

Nov 30, 2012, 3:59 AM

Post #6 of 7 (1993 views)
Re: [Sunchey] Change a file [In reply to] Can't Post

As far as I know, the only safe use if when you want to re-read the file that you have written.


FishMonger
Veteran / Moderator

Nov 30, 2012, 6:59 AM

Post #7 of 7 (1988 views)
Re: [Sunchey] Change a file [In reply to] Can't Post

If you open in read/write mode in order to change the file contents, you'll need to store the data in a var as you make the changes. When that's done you then need to use the seek function to reset the file pointer to the beginning of the file and then the truncate function to clear/delete the contents and then finally output the updated data to the file.

http://perldoc.perl.org/functions/seek.html
http://perldoc.perl.org/functions/truncate.html

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives