f



Need help converting unix text to windows text

I have been working on this for weeks.  Whatever my FTP client does to
translate the unix text file with each line ending in a LF character
back into a LF CR I need some code in Perl to this.

The task is, save data from an html form into a flatfile database.  I
have a Linux server. The program I have allows the user to search for a
text file (unix text) and download the file through their browser.  It
has to convert the little LF's to CRLF.  I have tried using some
people's suggestion on how to do this.  But it doesn't work yet.  Right
now it serves up the file because I have everything commented out! A
little messy, but the solution might be hiding in there! I just need to
know the right way to tweak it...

Any assistance is greatly appreciated.

Here's my code:

#!/usr/bin/perl
# This file is the one working
# This is called form the search page from option 1 in menu. You have
to login and search for a record.
# use a 1 to search for 1001.txt
use CGI ':standard';
use CGI::Carp qw(fatalsToBrowser);

my $file_location;
my $ID;
my @fileholder;

$subdirectory=param('subdirectory');
$ID = param('id');
$file_location = "../" . $subdirectory. "/" . $ID . "du.txt";
if ($ID eq '') {
print "Content-type: text/html\n\n";
print "You must specify a file to download.";
} else {
#open(FILE, "$file_location") || Error('open', 'file');
#binmode (FILE);   # will read file as is (binary) it need to send
images
#@data=<FILE>;
#close FILE;
#print "Content-type: text/html\n\n";
#print $file_location;
#print @data;
#exit;
open(DLFILE, "<$file_location") || Error('open', 'file');
binmode -f(DLFILE);
@fileholder = <DLFILE>;
close (DLFILE) || Error ('close', 'file');

#foreach $fileholder (@fileholder) {
#$fileholder =~ s|\012|\m\n|g;
#$fileholder =~ s|\012|$/|g;
#$fileholder = split(/\012/,$fileholder);
#}
#print "Content-type: text/html\n\n";
#foreach $fileholder (@fileholder) {
#print $fileholder;
#}
#exit;
open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
Error('open', 'file');
print LOG "$ID\n";
close (LOG);

#print "Content-type:application/x-download\n"; #paste your type
#binmode STDOUT; # i not shure than need it
#print join(//, @data); # send client

print "Content-Type:application/x-download\n";
print "Content-Disposition:attachment;filename=$ID\n\n";
print @fileholder
}

sub Error {
      print "Content-type: text/html\n\n";
	print "The server can't $_[0] the $_[1]: $! \n";
	exit;
}

0
sking623 (1)
3/22/2005 7:35:54 AM
comp.lang.perl.misc 33233 articles. 2 followers. brian (1246) is leader. Post Follow

2 Replies
662 Views

Similar Articles

[PageSpeed] 5

Hello,

sking623@aol.com wrote:
> I have been working on this for weeks.  Whatever my FTP client does to
> translate the unix text file with each line ending in a LF character
> back into a LF CR I need some code in Perl to this.
> 
A simple substitute of CR (Unix) to CRLF (DOS) shall do this.
Look at: http://www.cpan.org/authors/id/G/GO/GONZO/linebreaktool-0.2pre1.pl

(No, the script is not written by me)

regards,
Reinhard
0
Reinhard
3/22/2005 10:37:36 AM
sking623@aol.com <sking623@aol.com> wrote:

> Whatever my FTP client does to
> translate the unix text file with each line ending in a LF character
> back into a LF CR 


No, you need it back into CR-LF, the order matters.


> I need some code in Perl to this.


   s/\012/\015\012/;


> I have tried using some
> people's suggestion on how to do this.  But it doesn't work yet.  


Then you should followup to the article containing the non-working
suggestion so that we could help you fix it.


> #!/usr/bin/perl


You should ask for all the help you can get!

Especially before asking hundreds of people around the world to
look at it for you.

   use warnings;
   use strict;


> use CGI ':standard';


> $subdirectory=param('subdirectory');
> $ID = param('id');


Who will be filling out your web form?

Any-old-body on the Internet?

If so, you better have taint checking in your program before you
put it on a publicly available web server:

   perldoc perlsec


> $file_location = "../" . $subdirectory. "/" . $ID . "du.txt";


You should declare your variables in the smallest possible scope.

Variable interpolation _is_ concatenation, only easier to read:

   my $file_location = "../$subdirectory/${ID}du.txt";


> print "Content-type: text/html\n\n";


Here you tell the browser that you will be giving it HTML.


> print "You must specify a file to download.";


That doesn't look like HTML.

You shouldn't lie to the browser like that.


> open(DLFILE, "<$file_location") || Error('open', 'file');
> binmode -f(DLFILE);


It can't be *both* text and binary.

You must treat it as one or the other.

Which is it?



That isn't how you use binmode() anyway, have you read the docs for it?

   perldoc -f binmode


> @fileholder = <DLFILE>;

> #foreach $fileholder (@fileholder) {


Why do you need a "file holder" if you are going to process it
a line-at-a-time anyway?


> #$fileholder =~ s|\012|\m\n|g;


What character does the \m escape give you?

Why the "g" option when you only have a single line anyway?

Where did you get this "suggestion" from?


> #$fileholder =~ s|\012|$/|g;


If you are on a unixlike system, then that is a no-op.

What were you hoping that it would accomplish for you?


> open (LOG, ">>/home/mortga20/public_html/$subdirectory/log.txt") ||
> Error('open', 'file');
> print LOG "$ID\n";
> close (LOG);


You had better implement file-locking in your CGI program if
you don't want the log file to become corrupted.


> #print join(//, @data); # send client


That is a mighty strange looking 1st argument to join() ...


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Tad
3/22/2005 2:08:38 PM
Reply: