e$B%A%1%C%He(B #2516 e$B$,99?7$5$l$^$7$?!#e(B (by Yusuke E.)
e$B1sF#$G$9!#e(B
IO#reopen e$B$K$D$$$FD4$Y$F$$$k$N$G$9$,!"$5$C$Q$j$o$+$j$^$;$s!#e(B
$ cat foo.txt
1
2
3
1.8: e$BESCf0LCV$Ne(B File e$B$Ke(B reopen e$B$7$?$iFI$_9~$a$J$$e(B
f1, f2 = File.new(“foo.txt”), File.new(“foo.txt”)
f2.gets
f1.reopen(f2)
p f1.gets #=> nil
p f1.gets #=> nil
p f1.gets #=> nil
p f1.pos #=> 2
1.9: reopen e$B$7$?8e$Oe(B get e$BAa$$!$A!)e(B pos
e$B$N7k2L$,0c$&e(B
f1, f2 = File.new(“foo.txt”), File.new(“foo.txt”)
f1.reopen(f2)
p f1.gets #=> “1\n”
p f2.gets #=> nil
p f1.pos #=> 2
p f2.pos #=> 2
f1, f2 = File.new(“foo.txt”), File.new(“foo.txt”)
f1.reopen(f2)
p f2.gets #=> “1\n”
p f1.gets #=> nil
p f1.pos #=> 6
p f2.pos #=> 2
reopen e$B$NA[Dj%f!<%9%1!<%9$C$F$I$s$J$s$G$7$g$&$+!#e(B
dup2
e$B$N$h$&$J$b$N$+$H;W$C$F$$$?$N$G$9$,!"$=$s$J5sF0$G$b$J$$$G$9$h$M!#e(B
e$B%f!<%9%1!<%9<!Bh$G$O!"e(Breopen e$B$N0z?t$KEO$5$l$?e(B IO e$B$Oe(B
close e$B$7$F$7$^$&e(B
e$BJ}$,:.Mp$7$J$/$F$9$`$H;W$$$^$9!#e(B
e$B$G$-$l$P%f!<%9%1!<%9$N5DO@$NA0$K5sF0$,JQ$o$C$?M}M3$rGD0.$7$?$+$C$?e(B
e$B$N$G$9$,!"2?$,%P%0$G2?$,;EMM$+$o$+$j$^$;$s$G$7$?!#e(B
http://redmine.ruby-lang.org/issues/show/2516
 1
 p f1.pos  #=> 2
 f1.reopen(f2)
char buf[80];
printf("pos %d\n", ftell(f2));
pos 6
内部的ã«ã¯ struct FILE㌠struct _IO_marker ã‚’ãƒã‚¤ãƒ³ãƒˆã—ã¦ã„ã¦ã€ _IO_markerã¯å…±æœ‰ã€ãã£ã¡ã§pos管ç†ã—ã¦ã„る。
åå¿œãŒãªã„ã®ã§ã€èª°ã‚‚興味ãŒãªã„ã®ã ã¨åˆ¤æ–ã—ã¾ã™ã€‚é è—¤ã•ã‚“ã¨IRCã§èª¿æ•´ã®ã†ãˆ
kosakiãŒãƒœãƒ¼ãƒ«ã‚’æ¡ã‚‹ã“ã¨ã«ã—ã¾ã—ãŸã€‚
ç¾çŠ¶ io.dup ã‚‚posãŒç‹‚ã£ãŸå‹•ãã‚’ã—ã¦ã„ã‚‹ã®ã§ä¸€ç·’ã«ç›´ã™ã“ã¨ã«ãªã‚‹ã¨æ€ã„ã¾ã™ã€‚
ãŸã¶ã‚“ã€æœˆæœ«ã®Tokyo Ruby Kaigiã®ãƒãƒ³ã‚ºã‚ªãƒ³ã§Hello Worldã¨ã‹
æ•™ãˆã¦ã‚‚らã„ãªãŒã‚‰ã€å†…è·ãƒãƒƒã‚¯ã™ã‚‹äº‹ã«ãªã‚Šãã†ã€‚
2010年2月11日15:24 Yusuke E. [email protected]:
 2
 p f2.gets #=> “1\n”
ã§ãã‚Œã°ãƒ¦ãƒ¼ã‚¹ã‚±ãƒ¼ã‚¹ã®è°è«–ã®å‰ã«æŒ™å‹•ãŒå¤‰ã‚ã£ãŸç†ç”±ã‚’把æ¡ã—ãŸã‹ã£ãŸ
ã®ã§ã™ãŒã€ä½•ãŒãƒã‚°ã§ä½•ãŒä»•æ§˜ã‹ã‚ã‹ã‚Šã¾ã›ã‚“ã§ã—ãŸã€‚
Linuxã ã¨åŒç‰ã®ä»¥ä¸‹ã®Cプãƒã‚°ãƒ©ãƒ ãŒ
#include <stdio.h>
int main(void)
{
FILE *f1, *f2;
char buf[80];
f1 = fopen("inputfile", "r+");
printf("%s", fgets(buf, 80, f1));
f2 = freopen("inputfile", "r+", f1);
printf("%s", fgets(buf, 80, f1));
printf("%s", fgets(buf, 80, f1));
printf("%s", fgets(buf, 80, f1));
printf("pos %d\n", ftell(f1));
printf("pos %d\n", ftell(f2));
}
ã“ã†ãªã‚Šã¾ã™ã€‚
1
1
2
3
pos 6
pos 6
内部的ã«ã¯ struct FILE㌠struct _IO_marker ã‚’ãƒã‚¤ãƒ³ãƒˆã—ã¦ã„ã¦ã€
_IO_markerã¯å…±æœ‰ã€ãã£ã¡ã§pos管ç†ã—ã¦ã„る。
ã¾ãŸ SUSã‚’ã¿ã‚‹ã¨
http://www.opengroup.org/onlinepubs/009695399/functions/freopen.html
最åˆã«å¼•æ•°streamã‚’flushã—ã¦closeã™ã‚‹ã‚ˆã‚“。ã¨ã‹æ›¸ã„ã¦ã‚ã‚‹ã‘ã©ã€Linuxã¯å˜ã«dupã—ã¦ã‚‹ã ã‘ãªã‚“ã§closeã—ã¦ãªã„ã¿ãŸã„。ãªã‚“ã‹äº’æ›æ€§ã®ç†ç”±ãŒã‚ã‚‹ã®ã‹ãªï¼Ÿ
IOç³»ã¯è¦‹ã‚‹ã‹ã‚‰ã«æ¨™æº–Cライブラリã®å½±éŸ¿ãŒè‰²æ¿ƒã„ã®ã§ã€ã“ã‚Œã«åˆã‚ã›ã‚Œã°ã„ã„ã‚“ã˜ã‚ƒãªã„ã§ã—ょã†ã‹ãã‡ãƒ»ãƒ»ãƒ»
nalshã•ã‚“ã‚ãŸã‚ŠãŒæ‚²é³´ã‚’上ã’ã‚‹æã‚ŒãŒã‚ã‚Šã¾ã™ãŒã€‚
e$B1sF#$G$9!#e(B
2010e$BG/e(B2e$B7ne(B15e$BF|e(B13:25 KOSAKI Motohiro
[email protected]:
e$BH?1~$,$J$$$N$G!"C/$b6=L#$,$J$$$N$@$HH=CG$7$^$9!#1sF#$5$s$He(BIRCe$B$GD4@0$N$&$(e(B
kosakie$B$,%!<%k$r0.$k$3$H$K$7$^$7$?!#e(B
e$B8=>ue(B io.dup e$B$be(Bpose$B$,68$C$?F0$-$r$7$F$$$k$N$G0l=o$KD>$9$3$H$K$J$k$H;W$$$^$9!#e(B
IO e$B$H$+6=L#$J$$$N$G$h$m$7$/$*4j$$$7$^$9!#e(B
e$B$"$H!"e(BIRC e$B$GOC$7$?e(B pos
e$B$N$H$A68$C$?F0$-$K$D$$$F5-O?$G$9!#e(B
e$B!{e(B pos e$B$,Ii$K$J$ke(B
$ cat foo.txt
1
2
3
f1 = File.new(“foo.txt”)
f2 = File.new(“foo.txt”)
f2.gets
f1.reopen(f2)
f2.gets
f1.rewind
p f2.pos #=> -2
e$B!{e(B pos e$B$KI{:nMQ$,$"$ke(B
f1 = File.new(“foo.txt”)
f2 = File.new(“foo.txt”)
f2.gets
f1.reopen(f2)
f2.gets
p f1.pos #=> 6
p f2.pos #=> 4
p f1.pos #=> 4