Re: How to write to a file at certain position?
LI NGOK LAM wrote: You will probably want to open the file with a mode of +. Yes and thank you! That does what I want now. Thank you very much !! But I found new problem now. I did what I want if I try on a bitmap file, but for text file, my new contents will overwrite the whole file, what's that about That's right. And if you add two and two you will get four mo matter how badly you want five. Text is sequential. Character data, including that of which text is composed, may be treated as simple binary arrays, in which elements can be addressed at any point, but only by byte-for-byte replacement of existing data. Read [filename], Write [filename] and Append [filename] modes are sequential, text-oriented modes. None of them allow for in-place replacement of data. Binary [_] mode does allow for replacemnt in place. This replacement must be on a byte for byte basis, though. You can't have your cake and eat it too. If you are replacing data byte-for-byte, you can edit any kind of data in-place. If you need the flexibility to replace data with data of another length, add new data anywhere but at the end, or delete data anywhere but at the end, you MUST use sequential, mode, which will clobber the file being written to. Perl does have in-place editing modes, which may abstract the copying/renaming process somewhat. Nevertheless, the same work is done under the surface. Joseph or where I should refer to now? -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
How to write to a file at certain position?
Is there any methods or documents I can refer to learn how to write a file at any desire position with any length without to write the whole again ? I mean, I dont want to write to a new file, kill the old file, rename the new file, or read the old file, modify it and rewrite the old file etc... for example, for a mp3 file, I can modify the tags by its header location. So I just want to overwrite some bytes to the head of the file, then the rest are just remain the same Any suggestion are very apperciate =) Thanks in advice
Re: How to write to a file at certain position?
LI NGOK LAM said: Is there any methods or documents I can refer to learn how to write a file at any desire position with any length without to write the whole again ? I mean, I dont want to write to a new file, kill the old file, rename the new file, or read the old file, modify it and rewrite the old file etc... for example, for a mp3 file, I can modify the tags by its header location. So I just want to overwrite some bytes to the head of the file, then the rest are just remain the same Any suggestion are very apperciate =) Thanks in advice You want seek(), and possibly tell(). perldoc -f seek -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
You want seek(), and possibly tell(). perldoc -f seek Thanks for reply, but seems I have to clarify my question. 'seek' and 'tell' only helping me to target my position within a file handle. Say, if I have a 1MB file, and I just want to over write bytes from 0 to 1000 byte then my job is done, file is supposed to be saved. I want to avoid to rewrite the rest 900KBs again. Would you imagine what I am asking ? Welcome for any further suggestions. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
LI NGOK LAM said: You want seek(), and possibly tell(). perldoc -f seek Thanks for reply, but seems I have to clarify my question. 'seek' and 'tell' only helping me to target my position within a file handle. Say, if I have a 1MB file, and I just want to over write bytes from 0 to 1000 byte then my job is done, file is supposed to be saved. I want to avoid to rewrite the rest 900KBs again. Would you imagine what I am asking ? Welcome for any further suggestions. Open your file in read/write mode. Seek to where you want to be. Write the data. Close the file. You will probably want to open the file with a mode of +. perldoc -f open -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
This is a snippet from an cgi based file uploader I wrote once. I think this is what you're after #/!perl -w use strict; use IO::File; my $offset = 3; my $file_binary = fg; sysopen(OUTFILE, out.txt, O_WRONLY) or print Couldn't open file for read/write ($!)\n; binmode OUTFILE; sysseek OUTFILE, $offset, 0; syswrite OUTFILE, $file_binary, length($file_binary); close OUTFILE; out.txt goes from 1234567 to 123fg67 Li Ngok Lam [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] You want seek(), and possibly tell(). perldoc -f seek Thanks for reply, but seems I have to clarify my question. 'seek' and 'tell' only helping me to target my position within a file handle. Say, if I have a 1MB file, and I just want to over write bytes from 0 to 1000 byte then my job is done, file is supposed to be saved. I want to avoid to rewrite the rest 900KBs again. Would you imagine what I am asking ? Welcome for any further suggestions. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
You will probably want to open the file with a mode of +. Yes and thank you! That does what I want now. Thank you very much !! But I found new problem now. I did what I want if I try on a bitmap file, but for text file, my new contents will overwrite the whole file, what's that about or where I should refer to now? -- Paul Johnson - [EMAIL PROTECTED] http://www.pjcj.net -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
#/!perl -w use strict; use IO::File; my $offset = 3; my $file_binary = fg; sysopen(OUTFILE, out.txt, O_WRONLY) or print Couldn't open file for read/write ($!)\n; binmode OUTFILE; sysseek OUTFILE, $offset, 0; syswrite OUTFILE, $file_binary, length($file_binary); close OUTFILE; out.txt goes from 1234567 to 123fg67 Thank you veeery much ! It does what I want too, but I wonder why we have to make it a binmode while we are dealing with a text file ? Is that we must treat the FH is a binary source for whatever + or sysread/write ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: How to write to a file at certain position?
Hi Again, You're right, if we were dealing with a text file, you wouldn't have to use binmode. However, my original script **was** for uploading binarys, and you mentioned mp3, so it made sense to leave it in. Hope I've been of some help Rob Li Ngok Lam [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] #/!perl -w use strict; use IO::File; my $offset = 3; my $file_binary = fg; sysopen(OUTFILE, out.txt, O_WRONLY) or print Couldn't open file for read/write ($!)\n; binmode OUTFILE; sysseek OUTFILE, $offset, 0; syswrite OUTFILE, $file_binary, length($file_binary); close OUTFILE; out.txt goes from 1234567 to 123fg67 Thank you veeery much ! It does what I want too, but I wonder why we have to make it a binmode while we are dealing with a text file ? Is that we must treat the FH is a binary source for whatever + or sysread/write ? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]