Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: explanation for sysread and syswrite

by dbwiz (Curate)
on May 14, 2005 at 12:43 UTC ( #457036=note: print w/ replies, xml ) Need Help??


in reply to explanation for sysread and syswrite

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.


Comment on Re: explanation for sysread and syswrite
Re^2: explanation for sysread and syswrite
by bahadur (Sexton) on May 14, 2005 at 12:57 UTC
    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

        thanks for that
        so if i give the buffer size the same as the size of the file what would happen. would it write the whole thing to the scalar at once or what?
        and hey how do i change the mode back to normal.
        like one mode is binmode. what is the other mode? how do i undo binmode?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://457036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-09-18 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (124 votes), past polls