I am attempting to write an awesome perl script. Before it can do anything useful, it must first be able to read in a file, and write out a file (Note: I am NOT talking about text files). Here are the two subroutines I wrote to try to do this:
Expand|Select|Wrap|Line Numbers
- sub Load {
- my $file = &Prompt("File Name?");
- open(INFO, $file); # Open the file
- my @lines = <INFO>; # Read it into an array
- close(INFO);
- my $index = 0;
- my @buffer1;
- while (@lines) {
- $nextline = pop @lines;
- while (length($nextline) != 0) {
- $buffer1[$index] = ord chop($nextline);
- $index++;
- }
- }
- # It was loaded backward, so let's reverse it
- $bufferlength = $index;
- my @buffer2;
- for ($index=0; $index < $bufferlength; $index++) {
- $buffer2[$index]=pop(@buffer1);
- }
- @buffer2; #return the array
- }
- #####write to file#####
- sub WriteToFile {
- my $filename=&Prompt("new file name?");
- if ($filename eq "") {
- print "No FILE WRITTEN!";
- } else {
- open(INFO2, ">$filename"); # Open the file
- for ($index=0; $index < $bufferlength; $index++) {
- {
- use integer;
- $temp = $buffer[$index]*1;
- print INFO2 chr $temp; #####another problem is here
- }
- }
- close(INFO2);
- }
- }
This code seems to work perfectly 99% of the time. That 1% where it doesn't work irritates the heck out of me. After a week of tracking the bug I believe I found the problem:
When the file contains a byte that is 10 (in decimal, or NL in ascii), the WriteToFile function ends up writing 13 10 (or CR NL)--A carriage return is added. Also, when the load function reads a file that contains 13 10, only 10 is read--the carriage return is ignored (You should understand why this took me so long to find. I had to use another language to see what is happening).
Is there a way to stop this from happening? Is there a better way to load/save non-ascii files?