473,473 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Carriage Return is messing up my program

2 New Member
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 2368
miller
1,089 Recognized Expert Top Contributor
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 Recognized Expert Top Contributor
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
Detlev808
2 New Member
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
jabirahmed
5 New Member
#

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

Thank you. It works great now.
Jun 4 '07 #5
miller
1,089 Recognized Expert Top Contributor
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.