前提是path要存在也就是smallfile.txt要存在,fd是读取一次就会自增一个数字
path = Dir.pwd + "/smallfile.txt"
fd = IO.sysopen(path,"r+")
stream = IO.open(fd,"r+")
调用flush把写入的文件从缓冲区刷新到文件中
irb(main):057:0> stream = File.open(path,"r+")
=> #<File:/m8/ruby/smallfile.txt>
irb(main):058:0> stream.fileno
=> 18
irb(main):059:0> stream.pos
=> 0
irb(main):060:0> stream.getc
=> "a"
irb(main):072:0> stream.puts("y")
=> nil
irb(main):073:0> stream.flush
=> #<File:/m8/ruby/smallfile.txt>
irb(main):072:0> stream.puts("y")
=> nil
irb(main):073:0> stream.flush
=> #<File:/m8/ruby/smallfile.txt>
我们把pos放在最前面然后gets第一行第二行
我们获取获取字节,如果getbytes为空则我们重新定位pos位置为0从0开始
对于二进制文件的操作这样是很有效率的