情報学部 | 菅沼ホーム | 目次 | 索引 |
fd = IO.sysopen("file1") io = IO.new(fd, "r") p io.gets # => "data1 in file1\n"
IO.foreach("file1") { |line| p line } p IO.readlines("file1")
"data1 in file1\n" "data2 in file1\n" ["data1 in file1\n", "data2 in file1\n"]
p IO.read("file1", 5, 0) # => "data1"
IO.foreach("file1") { |line| p line } p IO.readlines("file1")
"data1 in file1\n" "data2 in file1\n" ["data1 in file1\n", "data2 in file1\n"]
fd = IO.sysopen("file1") io = IO.new(fd, "r") p io.gets # => "data1 in file1\n"
x = 10 + 20 $stdout << "結果は " << x << " です" # => 結果は 30 です
io = File.open("file1", "r") p io.gets # => "data1 in file1\n" io.close
io = File.open("file1", "r") io.each { |line| p line } io.close
"data1 in file1\n" "data2 in file1\n"
str = "" io = File.open("file1", "r") io.each_byte { |ch| str << ch } p str # => "data1 in file1\ndata2 in file1\n" io.close
p $stdin.fileno # => 0 p $stdout.fileno # => 1 p $stderr.fileno # => 2
str = "" io = File.open("file1", "r") while ch = io.getc str << ch end p str # "data1 in file1\ndata2 in file1\n" を出力 io.close
io = File.open("file1", "r") while line = io.gets p line end io.close
"data1 in file1\n" "data2 in file1\n"
io = File.open("file1", "r") io.lineno = 10 while line = io.gets print io.lineno, " ", line end io.close
11 data1 in file1 12 data2 in file1
str = "" io = File.open("file1", "r") io.pos = 16 while ch = io.gets str << ch end p str # "data2 in file1\n" を出力 io.close
inp = File.open("file1", "r") out = File.open("file2", "w") while line = inp.gets line = line.chomp # 改行を取り除く out.print(inp.lineno, " ", line, "\n") end inp.close out.close
1 data1 in file1 2 data2 in file1
inp = File.open("file1", "r") out = File.open("file2", "w") while line = inp.gets line = line.chomp # 改行を取り除く out.printf("%2d %s\n", inp.lineno, line) end inp.close out.close
1 data1 in file1 2 data2 in file1
inp = File.open("file1", "r") out = File.open("file2", "w") while ch = inp.getc out.putc ch end inp.close out.close
data1 in file1 data2 in file1
inp = File.open("file1", "r") out = File.open("file2", "w") while line = inp.gets out.puts(line, line) end inp.close out.close
data1 in file1 data1 in file1 data2 in file1 data2 in file1
inp = File.open("file1", "r") p inp.read() # => "data1 in file1\ndata2 in file1\n" inp.pos = 0 p inp.read(10) # => "data1 in f" inp.pos = 0 buf = "" p inp.read(10, buf) # => "data1 in f" p buf # => "data1 in f" inp.close
inp = File.open("file1", "r") p inp.readlines # => ["data1 in file1\n", "data2 in file1\n"] inp.close
out = File.open("file2", "w") $stdout.reopen(out) $stderr.reopen("file3", "w") x = 5 while x >= 0 y = 10 / x printf("%3d", y) x -= 1 end out.close
2 2 3 5 10 test.rb:6:in `/': divided by 0 (ZeroDivisionError) from test.rb:6
io = File.open("file1", "r") while line = io.gets print io.lineno, " ", line end io.rewind while line = io.gets print io.lineno, " ", line end io.close
1 data1 in file1 2 data2 in file1 1 data1 in file1 2 data2 in file1
io = File.open("file1", "r") while line = io.gets print io.lineno, " ", line end io.seek(0, IO::SEEK_SET) # io.pos = 0 と同じ while line = io.gets print io.lineno, " ", line end io.close
1 data1 in file1 2 data2 in file1 3 data1 in file1 4 data2 in file1
io = File.open("file1", "r") str = "" ch = io.getc str << ch ch = io.getc str << ch io.ungetc(ch) ch = io.getc str << ch p str # => "daa" io.close
io = File.open("file2", "w") p io.write("abc") # => 3 ( file2 には abc ) io.close
情報学部 | 菅沼ホーム | 目次 | 索引 |