472,982 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

Carriage Return is messing up my program

Hello,

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
  1. sub Load {
  2.     my $file = &Prompt("File Name?");
  3.     open(INFO, $file);    # Open the file
  4.     my @lines = <INFO>;    # Read it into an array
  5.     close(INFO);
  6.  
  7.     my $index = 0;
  8.  
  9.     my @buffer1;
  10.  
  11.     while (@lines) {
  12.         $nextline = pop @lines;
  13.  
  14.         while (length($nextline) != 0) {
  15.             $buffer1[$index] = ord chop($nextline);
  16.             $index++;
  17.         }
  18.     }
  19.  
  20.     # It was loaded backward, so let's reverse it
  21.  
  22.     $bufferlength = $index;
  23.  
  24.     my @buffer2;
  25.  
  26.     for ($index=0; $index < $bufferlength; $index++) {
  27.         $buffer2[$index]=pop(@buffer1);
  28.     }
  29.  
  30.     @buffer2; #return the array
  31. }
  32.  
  33.  
  34.  
  35. #####write to file#####
  36. sub WriteToFile {
  37.     my $filename=&Prompt("new file name?");
  38.  
  39.     if ($filename eq "") {
  40.         print "No FILE WRITTEN!";
  41.  
  42.     } else {
  43.         open(INFO2, ">$filename");        # Open the file
  44.         for ($index=0; $index < $bufferlength; $index++) {
  45.             {
  46.                 use integer;
  47.                 $temp = $buffer[$index]*1;
  48.  
  49.                 print INFO2 chr $temp; #####another problem is here
  50.             }
  51.         }
  52.         close(INFO2);
  53.     }
  54. }
  55.  
As you can see, the load subroutine reads the file and creates an array where each element corresponds to a byte in the file converted to integer form (they must be integers so I can manipulate them). The WriteToFile subroutine converts each integer in the array to a character, and then writes it to a file.

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?
Jun 3 '07 #1
5 2352
miller
1,089 Expert 1GB
Greetings Detlev,

Putting aside all best code practices advice and criticism concerning your method of implementation, what you need is binmode.

perldoc binmode

This is what lets you treat an input or output stream as binary versus text. Text gets special handling of the \n character for different operating systems. Just read the full documentation for the details.

- Miller
Jun 4 '07 #2
AdrianH
1,251 Expert 1GB
Hello,

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
  1. #####Load the file#####
  2. use strict;
  3. use warnings;
  4.  
  5. sub Load
  6. {
  7.   my $file=&Prompt("File Name?");
  8.   open(INFO, $file);        # Open the file
  9.   my @lines = <INFO>;        # Read it into an array
  10.   close(INFO);
  11.  
  12.   my $index=0;
  13.  
  14.   my @buffer1;
  15.  
  16.   while (@lines)
  17.   {
  18.  
  19.     $nextline=pop(@lines);
  20.  
  21.     while (length($nextline) != 0)
  22.     {
  23.       $buffer1[$index]=ord chop($nextline); 
  24.  
  25.       $index++;
  26.     }
  27.   }
  28.  
  29.   #It was loaded backward, so let's reverse it
  30.  
  31.   $bufferlength=$index;
  32.  
  33.   my @buffer2;
  34.  
  35.   for ($index=0; $index < $bufferlength; $index++)
  36.   {
  37.     $buffer2[$index]=pop(@buffer1);
  38.   }
  39.  
  40.   @buffer2; #return the array
  41. }
  42.  
  43.  
  44.  
  45. #####write to file#####
  46. sub WriteToFile
  47. {
  48.   my $filename=&Prompt("new file name?");
  49.   if ($filename eq "")
  50.   {
  51.     print "No FILE WRITTEN!";
  52.   }
  53.   else
  54.   {
  55.     open(INFO2, ">$filename");        # Open the file
  56.     for ($index=0; $index < $bufferlength; $index++)
  57.     {
  58.       {
  59.         use integer;
  60.         $temp=$buffer[$index]*1;
  61.  
  62.         print INFO2 chr $temp; #####another problem is here
  63.  
  64.       }
  65.     }
  66.     close(INFO2);
  67.   }
  68. }
  69.  
As you can see, the load subroutine reads the file and creates an array where each element corresponds to a byte in the file converted to integer form (they must be integers so I can manipulate them). The WriteToFile subroutine converts each integer in the array to a character, and then writes it to a file.

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?
Well, if you are relying on CR and/or LF as your record separator, looks to me like you are using a text file.

Try using read to read it all in, then use split with a regex of /(\r\n|\n\r|\n|\r)/ on the read in string. From there I think your read routine will work.

As for writing a scalar, I’m not sure.

BTW, please use [code=perl]...your code here...[/code] tags and INDENT to improve readability.


Adrian
Jun 4 '07 #3
Greetings Detlev,

Putting aside all best code practices advice and criticism concerning your method of implementation, what you need is binmode.

perldoc binmode

This is what lets you treat an input or output stream as binary versus text. Text gets special handling of the \n character for different operating systems. Just read the full documentation for the details.

- Miller
Thank you. It works great now.
Jun 4 '07 #4
#

#
$nextline=pop(@lines);
try a chomp($nextline);

Thank you. It works great now.
Jun 4 '07 #5
miller
1,089 Expert 1GB
Thank you. It works great now.
Glad I could help.

- Miller
Jun 4 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Canes_Rock | last post by:
The information posted at: ...
4
by: Dr. Laurence Leff | last post by:
I am writing a Java program to read in XML file, modify some elements slightly, and then write it out. That XML file is prepared in Docbook. It works fine, except that it is disturbing the...
4
by: Josh | last post by:
Hi, I'm using System.Data.DataSet.ReadXml to convert some xml from a webservice to a DataSet. The xml looks like: <?xml version="1.0"...
12
by: Nimmy | last post by:
Hi, I have a data file and I want to remove Carriage returns. Any one has any C code/program which does this? I am working on Windows XP machine.....I don't have access to UNIX machine. But I...
6
by: Laura D | last post by:
How can I identify a carriage return in C++? \r, \f, \0, \n, \t does not work. I have also tried !isprint(ch), iscntrl(ch), isspace(ch), etc....with no luck! I even poked around in the MSDN and...
5
by: Dixie | last post by:
Can I program a label with a carriage return to put the 2nd part of the label on a new line. The only way I can see of doing this is to convert the label to a text box, then use chr$(13). dixie
1
by: soriold | last post by:
Hey there: I am done writing a rather large program for school and the only thing that is still driving me nuts is how to exit the program based on the fact that the return/carriage key was...
1
by: nur123 | last post by:
Thanks in advance who will look at it. I have been encountering an issue which I can’t find a way out of it. What my pgm does: It (java codes) reads oracle table data and creates flat text...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.