Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting the binary mode in a file open. 1

Status
Not open for further replies.

qwert231

Programmer
Sep 4, 2001
756
US
Get this error:
D:\Perl Files>perl test.pl
Unknown open() mode '<:raw' at test.pl line 6.

When I try to run this code:
open(LAYOUT, &quot;<:raw&quot;, $pathS);

What am I doing wrong? Got 5.6.1 on WinXP.
 
To read binary files, you want to use binmode.

[tt]open (FILE, &quot;<c:/path/to/file.ext&quot;);
binmode FILE;
#process file
close (FILE);[/tt]
 
You should just need to do:
Code:
open(LAYOUT, $pathS);
binmode( LAYOUT );
From the perlfunc man pages:

binmode FILEHANDLE
Arranges for the file to be read or written in &quot;binary&quot;
mode in operating systems that distinguish between
binary and text files. Files that are not in binary mode
have CR LF sequences translated to LF on input and LF
translated to CR LF on output. Binmode has no effect
under Unix; in MS-DOS and similarly archaic systems, it
may be imperative--otherwise your MS-DOS-damaged C
library may mangle your file. The key distinction
between systems that need binmode and those that don't
is their text file formats. Systems like Unix and Plan9
that delimit lines with a single character, and that
encode that character in C as '\n', do not need
`binmode'. The rest need it. If FILEHANDLE is an
expression, the value is taken as the name of the
filehandle.


Cheers, Neil
 
K. That works, but it didn't. What is happening is that when I pull this file in I drop it into an array... to work on it. Then once I modify the various parts of the array, I drop it back into a file. But what is happening is that when I do this, the new file has those box things... wierd characters and spaces... I thought that binmode would fix it, but it doesn't. Any ideas?
 
Hmmm... I see... I still have a problem tho. I still get those funky boxes. Noticed something else. It doesn't do this on all files?! Just the ones I need to parse!
Here is my whole code:

$layout = '810-10-MP-2.txt';
$pathS = '//Dell2400/KPDP2/Layouts/10 Inch MP-2/'.$layout;
open(LAYOUT, &quot;< $pathS&quot;);
binmode LAYOUT;
@file_data = <LAYOUT>;

print &quot;$#file_data\n&quot;;
open JOB, &quot;>>jobfile.txt&quot;;
foreach(@file_data) {
print &quot;$_&quot;;
print JOB &quot;$_&quot;;
if($_ =~ m/FileType:/){
print $_;
}
}
#print JOB @file_data;
close JOB;
close LAYOUT;
 
Why does my text file come out like this when I pull it into my app:

FileType: Layout
&#2573;&#19712;㸒“ eNªQ&#14848;&#8192;&#8192;&#8192;¿E&#19712;㸒“ eNªQ&#11776;¬Hí¸¬H¿E&#3328;&#3328;
&#2573;&#17408;㸬H㸠eÅÊ’“N e²óªQ&#14848;&#8192;&#8192;&#8192;&#12544;&#3328;&#3328;
&#2573;†eœ`—µ¬HNêÀ›ÅÊ㸪Q®I eÅÊ&#14848;&#8192;&#8192;&#8192;¿E&#18688;œ`’“èÙÅʪQ¿E&#3328;&#3328;
&#2573;…¸ e㸒“ÅÊ&#14848;&#8192;&#8192;&#8192;¿E&#17920;¿E&#3328;&#3328;
&#2573;†eªQÅÊ’“NšÑN e¬H e㸜`ªQêÀN e›ªQ&#14848;&#8192;&#8192;&#8192;¡¡&#3328;&#3328;
&#2573;&#19712;㸗µœ`&#14848;&#3328;&#3328;
Autocrop:
 
If the file is binary, then you should really be treating it as if it contains nice newline characters to delimit lines. WHat you should do is slurp the file in to a single scalar, then match just part you need:
Code:
open( FILE, $in ) or die;
binmode FILE;
undef $/;
$data = <FILE>;
if( $data =~ /Filetype:(\w+)/ ) {   # or similar regexp
    print $1;
}
Chees, Neil :)
 
Here is a portion of the original file:
FileType: Layout
Macros: ~Macros.txt~

Datarecords: 1

Unitofmeasure: ~Inches~

Trace: ~F~

Usecolortransforms: 0

Main:
...
There are no spaces at the end of those lines that Notepad could see. It is a text file. I am doing binmode LAYOUT (that is my filehandle). Slurping it as suggested by Neil does the same thing. The file is created by DP2 Kodak's Digital Production software. (they have developed their own scripting language) But the layouts are supposed to be plain-text. Could it be some form of Unicode?
 
Could it be the way Perl is writing the other file? Acording to Programming Perl (3rd edition) you can specify the binary mode and other I/O Disciplines with commands such as:
open(FH, &quot;>>:raw&quot;, $path) or Die &quot;can't open $path: $!&quot;;

But I can't get this to work... with 5.6.1. I get an unknown mode error.
 
Sorry guys, I am an idiot. Duh. LightElf, you were right. I needed to set the output file to binmode. Duh. Didn't realize that that was what you meant... K. Time for me to have my morning coffee. Thanks guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top