Welcome to WebmasterWorld Guest from 23.22.46.195

Forum Moderators: coopster & jatar k & phranque

Create a Unix file on Windows system

How to create

   
8:07 pm on May 7, 2007 (gmt 0)

10+ Year Member



I run a Perl script on a Windows system and i need to create a Unix file. I use the following line to create the file:

open (OUT,">$out") ;

$inline = join ("\",$mast[0],$mast[1],$mast[2]) ;

print OUT "$inline\n" ;

close (OUT) ;

The final result is a DOS file, but I need a UNIX file. The result must be transfert to another system.

Gilles

9:19 pm on May 7, 2007 (gmt 0)

5+ Year Member



that should be fine for unix, whats the problem?
9:58 pm on May 7, 2007 (gmt 0)

10+ Year Member



That the problem. The final result is a DOS file and not a Unix file. I use Activestate for Perl interpreter, Windows XP and Sambar as the server.
10:44 pm on May 7, 2007 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



you could be confusing the file with the file system.
what happens when you transfer that file to a *nix-type file system?
3:42 am on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



When you use perl to create a file on a DOS system, it will create the DOS end-of line character. I cannot remember the specifics, but I **think** this is a CRLF (carriage return + line feed.) Unix-based systems create only the single line feed.

I first experienced this way back in the day when I did my scripts in NotePad, uploaded them ASCII to a server, and they would 500 error. A wise admin recommended I find an editor that saves in Unix format and volia, the problem went away.

Those of you more adept at perl-y stuff may have better recommendations than below. Try this, which is the octal equivalent of line feed:

$CRLF = "\x0d\x0a";

print OUT "$inline$CRLF" ;

It doesn't work if you put \x0d\x0a in the print command for some reason. Yo might also try just \x0d and \x0a independently.

Of course it's possible none of it will work, since you're creating the file in DOS it may try to adhere to the OS in printing out the file, but it's worth a try. If all else fails you could open it in HomeSite or some other program that saves in Unix format and do a save as.

6:16 am on May 8, 2007 (gmt 0)

5+ Year Member



He is using perl to write the file and using a newline as the line ending. Windows is not going to add a carraige return to that. There should be no problem with the line endings when used on a nix box. Something else is wrong or the OP is confused.
7:06 am on May 8, 2007 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



you might need to use the binmode() function [perldoc.perl.org] in this case.
9:40 am on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Yes, DOS uses
0D0A
for end of line CR + LF. UNIX only uses
0A
. Annoys the hell out of me when a friend of mine sends me code!

You'll need to binmode OUT as follows:

open (OUT,">$out");  
binmode OUT;

Do that immediately. Then instead of doing:

print OUT "$inline\n";

Try:

print OUT "$inline\x0a";

I think \x0a is the same as \r, on DOS you're supposed to use \r\n. I vaguely remember in the olden days I used to have to do that, now \r is inserted automatically.

Infact, thinking about it, you shouldn't might not need to binmode OUT either if you can use \r.

Try those things and let us know how that goes.

2:50 pm on May 8, 2007 (gmt 0)

10+ Year Member



It's work now.

I just add
binmode OUT ;
after
open (OUT,">$out") ;

I stay with

print OUT "$inline\n" ;

for each line and this work fine.

Thank for advise.

3:02 pm on May 8, 2007 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I think then by changing to a binary stream, the DOS filesystem is not adding the \r (0x0D) that unix doesn't like.

A good learning experience for all!

 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month