Gossamer Forum
Home : General : Perl Programming :

Why an Incomplete File Copy?

Quote Reply
Why an Incomplete File Copy?
Can anyone tell me why I'm getting what appears to be a false EOF during a file read using the [read{}] function?

The read file opens OK
The write file opens OK
The original file size is 1570 bytes.
The copied file size is 115 bytes. (Copy appears to terminate on byte value = 26)


The script is:

$path_to_file1 = "transfers\\hello_world.pdf";
$path_to_newfile = "database\\hello_world.out";


# start displaying the HTML page
print "Content-type: text/html\n\n";
print "<html><head></head>\r\n";
print "<body>\r\n";

open (READFILE, "$path_to_file1") || &errorfunc("Couldn't open the file [$path_to_file1] to read.");
print "The file [$path_to_file1] was opened for reading<br>";
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat $path_to_file1;
print "The size of file [$path_to_file1] is $size<br>";

open (WRITEFILE, ">$path_to_newfile") || &errorfunc("Couldn't open the file [$path_to_newfile] to write.");
print "The file [$path_to_newfile] was opened for writing<br>";


while (read READFILE, $buf, 16384) {
print WRITEFILE $buf;
}

close (READFILE);
close (WRITEFILE);

print "<br><br>Finished copying from [$path_to_file1] to [$path_to_newfile]<br>";
($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat $path_to_newfile;
print "Size of [$path_to_newfile] is $size";

print "<br><br></body></html>";
exit;

}


sub errorfunc {
@args = @_;
print "$args[0]<br>";
print "<br><br></body></html>";
exit;
}
Quote Reply
Re: [alsmall] Why an Incomplete File Copy? In reply to
You are reading AND writing binary data.

You must tell perl to give your filehandles the proper semantics.

binmode READFILE;

binmode WRITEFILE;

(no explicit DISCIPLINES means binary (or "raw") semantics are used.)



This must be done after your 'open' functions but before you do any I/O.
Quote Reply
Re: [kinetik] Why an Incomplete File Copy? In reply to
CoolThank you, K. That was the missing link. It works fine now.
Quote Reply
Re: [alsmall] Why an Incomplete File Copy? In reply to
The file size differences are probably due to non printing characters that are in the PDF file that didn't make it to the out file.



Kinetik