From: daz Date: 2003-06-04T13:15:02+09:00 Subject: Re: file I/O: how can I disable auto-convert of the 2-byte sequence 0x0D, 0x0A into a single byte '\n'? file I/O: how can I disable auto-convert of the 2-byte sequence 0x0D, 0x0A into a single byte '\n'? "Polscheit, Frank" wrote: >Hi, >I want to process binary data, which can contain such byte sequences > but not indicating end-of-line as the file has a fix record length. >Best regards, >Frank And here's an example. #--------- FIXREC_LEN = 10 FIXPAD_CHR = '!' File.open('result.dat', 'wb') do |fo| File.open('test.dat', 'rb') do |fi| until fi.eof dat = fi.read(FIXREC_LEN) p [dat, dat[4], dat[5]] dat_new = dat.gsub(/\r\n/, "\n\r") fo.write(dat_new << FIXPAD_CHR * (FIXREC_LEN-dat.size)) end end end # files close automatically for File.open {block} puts '-' * 30 File.open('result.dat', 'rb') do |fi| until fi.eof dat = fi.read(FIXREC_LEN) p [dat, dat[4], dat[5]] end end #--------- Output: ["ERST\r\nZWEI", 13, 10] ["T\r\nDRITT\r\n", 82, 73] ["VIERT\r\n", 84, 13] ------------------------------ ["ERST\n\rZWEI", 10, 13] ["T\n\rDRITT\n\r", 82, 73] ["VIERT\n\r!!!", 84, 10] #File dumps: ## test.dat # 000: 45 52 53 54 0d 0a 5a 57 45 49 ERST..ZWEI # 010: 54 0d 0a 44 52 49 54 54 0d 0a T..DRITT.. # 020: 56 49 45 52 54 0d 0a VIERT.. ## result.dat # 000: 45 52 53 54 0a 0d 5a 57 45 49 ERST..ZWEI # 010: 54 0a 0d 44 52 49 54 54 0a 0d T..DRITT.. # 020: 56 49 45 52 54 0a 0d 21 21 21 VIERT..!!! daz