File: file_io.rb

package info (click to toggle)
ruby-cstruct 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 412 kB
  • sloc: ruby: 1,008; makefile: 7
file content (48 lines) | stat: -rw-r--r-- 872 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# CStruct Examples
require 'cstruct'

# struct Point in Ruby: 
class Point < CStruct
  int32:x
  int32:y 
end

# struct PointF in Ruby: 
class PointF < CStruct
  double:x
  double:y 
end

class Addition < CStruct
    char :description,[32]
end

# write file
File.open("point.bin","wb")do |f|
  point   = Point.new {|st| st.x = 100; st.y =200 }
  point_f = PointF.new{|st| st.x = 20.65; st.y =70.86 }
  
  addition = Addition.new 
  addition.description= "Hello Ruby!"

  f.write point.data
  f.write point_f.data
  f.write addition.data
end

#read file
File.open("point.bin","rb")do |f|
  point    = Point.new 
  point_f  = PointF.new
  addition = Addition.new
  
  point   << f.read(Point.size)
  point_f << f.read(PointF.size)
  addition <<  f.read(Addition.size)
  
  puts point.x
  puts point.y
  puts point_f.x
  puts point_f.y
  puts addition.description.to_cstr
end