Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

explanation for sysread and syswrite

by bahadur (Sexton)
on May 14, 2005 at 12:18 UTC ( [id://457032]=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: explanation for sysread and syswrite
by dbwiz (Curate) on May 14, 2005 at 12:43 UTC

    Seriously, bahadur,

    You should read How (Not) To Ask A Question.

    It it seems too much, here's the bottom line: We are here to help, (also because many of us learn something new every day when we help solving problems). But to solve your problem we require that you play by the rules and show at least some effort.

    Asking us to help without even giving a hint of what you have done, looks definitely out of order.

    Please read the guidelines and try again.

      ok here is my code
      print "Opening the file\n"; open(F,"<$pair1[1]"); print "changing the mode to bin\n"; binmode F; #binmode $new_sock; my $bufsize = 100; my $buffer=0; my $n = sysread(F,$buffer,$bufsize); print "the value of n is $n\n"; close(F);
      my problem starts with the line my $n = sysread... the third last line. the $bufsize is the amount of bytes to be read from the file. because every file will be of a different length so i cant know how much the value will be. so how do i know the size of a file in bytes.
      the next problem is receiving the data on the client side.

      can any one write a piece of code how to receive the data on the client end.

        That's better.

        A few comments:

        • Always check the result of a file open or I/O operation.
        • You don't need to know the size of your file to use sysread. You can say how much sysread should read, and that function will return the number of bytes read, or 0 when it's finished.
        • If you really need to know the file size, use a -s check.

        Here is an example.

        #!/usr/bin/perl use warnings; use strict; my $filename = "stable.tar.gz"; # it's the latest Perl source code die "filename not found\n" unless -f $filename; my $size = -s $filename; my $total_read = 0; open F, "< $filename" or die "can't open $filename\n"; my $bufsize = 2_000_000; my $buffer; while ( my $read = sysread(F , $buffer , $bufsize, ) ) { printf "Read %8u bytes\n", $read; # do something with $buffer $total_read += $read; } print "initial size: $size\n"; print "total read: $total_read\n"; __END__ output: Read 2000000 bytes Read 2000000 bytes Read 2000000 bytes Read 2000000 bytes Read 2000000 bytes Read 2000000 bytes Read 254916 bytes initial size: 12254916 total read: 12254916

        HTH

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-19 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found