I am reading a binary file and I am using the tell() function to get the specific position of a location. I am reading the file in binary mode:

open( FH , "+<" , $source ) or die "Can not open file: $source $!\n"; binmode(FH);

I am moving the pointer to the desired position with string condition and using:

$position = tell(FH)

In order to get the pointer position int. Then I use SEEK to move to the position that I have previously marked by doing:

seek( FH , $position , SEEK_SET );

Where I am issuing the commang:

write( FH, $example , $example_size ) or die "Couldn't write at FH : $!\n";

I was browsing over the internet and I found that the opposite of read() is not write(). But later on trying to find a solution to my problem I came across with the Perl man page perlapio − perl’s IO abstraction interface. and also perlclib.

Both of the web pages state that the alternative of fwrite() in C is write():

fwrite(p, size, n, stream) PerlIO_write(perlio, buf, numbytes)

I tried to apply that in my code and I keep getting this error:

Useless use of a constant (FH) in void context at test.pl line 193.

I looked online what is the error and I found that it can not open the file properly. I do have a die condition on open and also I have a few printf() conditions to test if the file is opening and they are coming up. So at this point I assume it is a wrong syntax with the write() function. Are there any alternatives that I could use?

Thanks in advance for your time and effort.


In reply to Writing at specific position and length in binary file. by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.