1. converting carriage return
- Posted by D & B Edmunds <dae at PACLINK.COM> Jun 15, 1997
- 697 views
Hello to group! I am trying to encrypt a file, using a simple xor routine on each byte in it. I find that carriage return-line feed is not converted correctly using getc(), and can't figure out a fast way to do this. -- my code procedure input() integer file_in integer file_out char char_in char char_out file_in = open("x.in", "r") --file must exist file_out = open("x.out","w") --output file --loop through x.in while 1 do char_in = getc(file_in) if char_in = -1 then exit --get out of the loop at end of file end if char_out = xor_bits(char_out, #10) -- write to file puts(file_out,char_out) end while close(file_in) close(file_out) puts(1,"DONE!") end procedure -- If I do a simple gets, puts the two files are the same, but if I try to convert each 'char', then the carriage return/line feed combination is not separated into two bytes, and is not written correctly (the x0D part (carriage return)) is lost. If you can help please email me at dae at paclink.com. Thanks.
2. Re: converting carriage return
- Posted by Lucius L Hilley III <luciuslhilleyiii at JUNO.COM> Jun 15, 1997
- 693 views
- Last edited Jun 16, 1997
On Sun, 15 Jun 1997 17:39:21 -0700 D & B Edmunds <dae at PACLINK.COM> writes: > >Hello to group! > >I am trying to encrypt a file, using a simple xor routine on each >byte in it. >I find that carriage return-line feed is not converted correctly using >getc(), >and can't figure out a fast way to do this. >--snip > file_in = open("x.in", "r") --file must exist > file_out = open("x.out","w") --output file USE: file_in = open("x.in", "rb") file_out = open("x.out","wb") Open the files in binary mode.
3. Re: converting carriage return
- Posted by Daniel Berstein <architek at GEOCITIES.COM> Jun 15, 1997
- 670 views
- Last edited Jun 16, 1997
D & B Edmunds wrote: > procedure input() > integer file_in > integer file_out > char char_in > char char_out > file_in = open("x.in", "r") --file must exist ^^^^ WRONG! > file_out = open("x.out","w") --output file ... etc.... Your problem is that you opened the file as "text read" by means of the open("x.in","r"), Euphoria automatically converts \n chars. You should use the "rb" option that opens the file as "binary", so no conversion is made and you'll get the two byte pair you expect. Notice that you should also open the output file as binary ("wb") so there is no chance that Euphoria makes any change. Hope this helps you ;) -- Regards, Daniel Berstein architek at geocities.com http://www.geocities.com/SiliconValley/Heights/9316