Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

File Truncation Question

by arrow (Friar)
on May 24, 2003 at 03:46 UTC ( [id://260525]=perlquestion: print w/replies, xml ) Need Help??

arrow has asked for the wisdom of the Perl Monks concerning the following question:

I have been using ">" operator in the open function to first pull data, edit it, and then open (while truncating) again to write. My problem is that when I go to write the edited data, there is a single whitespace character as the very first character in the file. Another renegade whitespace is found nowhere else in the file, and I've done some testing and I've found it's only when I use open with the ">" that the extra space occures. What is wrong with what I am doing? The data is simple text in lines, and I am writing to the file using a foreach loop. Any help would be greatly appreciated.

Just Another Perl Wannabe

Replies are listed 'Best First'.
Re: File Truncation Question
by jonnyfolk (Vicar) on May 24, 2003 at 04:42 UTC
    If you don't post code for something like this it means you think there is something wrong with perl, and not with what you are doing :).

    Strip down your script to just the open statement and look at it again. If you still can't see what's wrong, then post the code so we can all see it.

    In the meantime the following works for me (no added whitespace):

    #!/usr/bin/perl -w use strict; use CGI ':standard'; open FH, '> data.txt' or die $!; print FH 'testing123'; close FH; print header(), start_html(-title => "test"); print "finished"; print end_html();
    Hope that helps...
Re: File Truncation Question
by Mr. Muskrat (Canon) on May 24, 2003 at 14:15 UTC

    open(FILE, '>', $file); truncates $file and opens it for writing.

    open(FILE, '>>', $file); opens $file for appending.

    open(FILE, '<', $file); opens $file for reading. See open in perlfunc for more information.

Re: File Truncation Question
by arrow (Friar) on May 26, 2003 at 02:30 UTC
    Wow, I was just in the process in posting my code, which happens to be

    open(DATA, ">/host/n/b/p/p/o/r/nbpdexplorers.port5.com/cgi-bin/data2.t +xt") or die "Cannot open database file: $!"; flock(DATA, 2); seek(DATA, 1, 0); foreach my $item (@currentdata) { print DATA "$item\n"; } flock(DATA, 8); close(DATA);

    and I realized the only thing which was different from the other examples you gave me. I had been using the seek(DATA, 1, 0); because another monk (who I don't remember, sorry) had advised to use when writing to a file and using flock. I tried it without that line and it worked fine. Thanks so much for your examples and assistance.

    Just Another Perl Wannabe
      Aha! That's the bit of code you should have included in the first place :)

      Reading up on seek, which I hadn't used before, what you seem to be doing is correctly opening your file, and then searching in the file for the point where you wish to begin.

      seek(DATA, 1, 0);
      is actually telling perl to offset the file by 1 byte and since it's a newly created file it simply creates the offset before it does anything else, hence the initial white space.

      Normally you would use seek for fixed length records where you can be sure that the beginning of a particular record can be found at an exact number of bytes into the file, so you were either told this for a specific purpose which you took to be standard procedure, or you were just told wrong! :)

      As I mentioned this is from my reading and interpretation and I rather imagine I will be corrected by other more experienced monks so please read for yourself and draw your own conclusions rather than rely on mine!

      I hope this has been helpful to you...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://260525]
Approved by blokhead
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-24 06:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found