Simple question about binmode

E

emcee2k

When I run the following code, it creates a 1063 byte file on Win32:

open(F, 'textfile.txt');
read(F, $txtPreview, 1024, 0);
close(F);
open(TL, '>textpreview.txt');
print TL $txtPreview;
close(TL);

There's 39 line breaks in the file it creates, so that explains the
file size of 1063 instead of 1024. However, with this code I expected
to get a file of 1024, but I still got 1063:

open(F, 'textfile.txt');
binmode(F);
read(F, $txtPreview, 1024, 0);
close(F);
open(TL, '>textpreview.txt');
print TL $txtPreview;
close(TL);

So what do I have to do to insure that the length argument to the read
function is treated as the amount of bytes to be read, rather than the
amount of characters?
 
T

Tad J McClellan

with this code I expected
to get a file of 1024, but I still got 1063:

open(F, 'textfile.txt');
binmode(F);
read(F, $txtPreview, 1024, 0);
close(F);
open(TL, '>textpreview.txt');
print TL $txtPreview;
close(TL);


You have not binmode()ed your output filehandle...

read() has a return value (as does open() and close()).

You should check the return values.

The length() function will tell you the size of $txtPreview.
 
G

Gunnar Hjalmarsson

When I run the following code, it creates a 1063 byte file on Win32:

open(F, 'textfile.txt');
read(F, $txtPreview, 1024, 0);
close(F);
open(TL, '>textpreview.txt');
print TL $txtPreview;
close(TL);

There's 39 line breaks in the file it creates, so that explains the
file size of 1063 instead of 1024.

Then textfile.txt is most likely *nix formated.
However, with this code I expected
to get a file of 1024, but I still got 1063:

open(F, 'textfile.txt');
binmode(F);
read(F, $txtPreview, 1024, 0);
close(F);
open(TL, '>textpreview.txt');
print TL $txtPreview;
close(TL);

It's TL you need to binmode to prevent Win32 from adding \015 to each line.
So what do I have to do to insure that the length argument to the read
function is treated as the amount of bytes to be read, rather than the
amount of characters?

I don't think your analysis is correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top