# .getc.chr doesn't work

**URL:** <https://www.ruby-forum.com/t/getc-chr-doesnt-work/149726>\
**Category:** Ruby\
**Created:** [September 29, 2008, 3:01am UTC](https://www.ruby-forum.com/t/getc-chr-doesnt-work/149726 "2008-09-29T03:01:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Procek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Procek](https://www.ruby-forum.com/u/Procek)\
**Post date:** [September 29, 2008, 3:01am UTC](https://www.ruby-forum.com/t/getc-chr-doesnt-work/149726/1 "2008-09-29T03:01:04Z")

</div>

Hi!

I’m trying to read a file using ruby. Firsty I use readline twice,  
then I wan’t to read some part of data, but I don’t get where I have a  
mistake. Could you help me find it?

######## CUT HERE ########  
#Constans  
FS = 0x1c  
RS = 0x1e

#Open file  
f = File.new(“abcdef”, “r”)

#Read a line, split it, and delete bad fields (last one)  
tbl = f.readline(FS.chr).split(RS.chr) - [FS.chr]  
#For each from tbl  
tbl.each { |x|  
#Split field  
splitted = x.split("\_")  
#The read loop  
tmp = String.new  
i=0  
while (i \< splitted[2].to\_i)  
tmp += f.getc.chr #Here I have an error…  
end  
#Save readed data  
f2 = File.new(s[0]+"-"+s[1]+".txt", “w”)  
f2.write(tmp)  
f2.close  
}

#Close file  
f.close  
######## CUT HERE ########

The test file in HEX (beacouse it have non-printable chars)  
######## CUT HERE ########  
31 37 31 5f 35 31 32 5f 31 32 1e 33 30 34 5f 35 31 32 5f 39 1e 1c 61  
62 63 64 65 66 67 68 69 6a 6b 6c 30 31 32 33 34 35 36 37 38  
######## CUT HERE ########

with greetings  
Procek

---

<div class="post-metadata">

**Author:** ![Procek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Procek](https://www.ruby-forum.com/u/Procek)\
**Post date:** [September 29, 2008, 5:56am UTC](https://www.ruby-forum.com/t/getc-chr-doesnt-work/149726/2 "2008-09-29T05:56:32Z")

</div>

On Sun, Sep 28, 2008 at 7:59 PM, Procek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> tmp = String.new  
> #Close file  
> f.close  
> ######## CUT HERE ########
> 
> The test file in HEX (beacouse it have non-printable chars)  
> ######## CUT HERE ########  
> 31 37 31 5f 35 31 32 5f 31 32 1e 33 30 34 5f 35 31 32 5f 39 1e 1c 61  
> 62 63 64 65 66 67 68 69 6a 6b 6c 30 31 32 33 34 35 36 37 38  
> ######## CUT HERE ########

I’m not sure exactly what you are trying to do, but I have a guess. I  
wouldn’t really use readline or getc for this…

FS = 0x1c  
RS = 0x1e

#your hex string  
hex = “31 37 31 5f 35 31 32 5f 31 32 1e 33 30 34 5f 35 31 32 5f 39 /  
1e 1c 61 62 63 64 65 66 67 68 69 6a 6b 6c 30 31 32 33 34 35 36 37 38”

#change it to your real data  
s = hex.split.map {|i| i.to\_i(16).chr}.join  
#=\> “171\_512\_12\036304\_512\_9\036\034abcdefghijkl012345678”

info, data = s.split(FS.chr)  
lengths = info.split(RS.chr).map {|i| i.split(/\_/).last}  
data\_array = []  
lengths.each {|len| data\_array \<\< data.slice!(0, len.to\_i)}

p data\_array

…Your test file isn’t long enough for me to establish the actual  
layout, so I had to make educated guesses. When reading a file you  
can wrap your code up like…

File.open( ‘my\_file’, ‘r’) do |f|  
#parsing code in here  
end

hth,  
Todd

---

<div class="post-metadata">

**Author:** ![Procek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Procek](https://www.ruby-forum.com/u/Procek)\
**Post date:** [September 29, 2008, 6:56am UTC](https://www.ruby-forum.com/t/getc-chr-doesnt-work/149726/3 "2008-09-29T06:56:13Z")

</div>

Hi,

At Mon, 29 Sep 2008 09:59:18 +0900,  
Procek wrote in [ruby-talk:316286]:

> i=0  
> while (i \< splitted[2].to\_i)  
> tmp += f.getc.chr #Here I have an error…  
> end

This is an inifinite loop.

```
splitted[2].to_i.times do
end

```

or

```
tmp = f.read(splitted[2].to_i)

```

work.

FS = “\x1c”  
RS = “\x1e”

File.open(“abcdef”, “rb”) do |f|  
f.gets(FS).chomp(FS).split(RS).each do |x|  
s = x.split("\_")  
tmp = f.read(s[2].to\_i)  
File.open(s[0]+"-"+s[1]+".txt", “w”) {|f2| f2.write(tmp)}  
end  
end
