fgetc引用变量

时间:2011-10-26 15:38:51

标签: php eof fgetc

我正在玩一些有趣的代码高尔夫,我遇到了一个问题:

Golfed:

<? while($v=fgetc(STDIN)){echo$v?$v:'';while((int)$v)echo--$v?$v:'';}

ungolfed:

<? 
while ( $v = fgetc(STDIN) ) {
    echo ($v ? $v : '');
    while ( (int) $v ) { echo (--$v ? $v : '' ); }
}
?>

在循环浏览文件的同时,我似乎非常快地点击了EOF,几乎就像我在$v的迭代转发fgetc一样,好像它是一个指针。


输入:

Recent endless problems

    * 463. Enumeration of divisor
    * 469. hexdump
    * 470. hexdump2
    * 484. Double Square Numbers
    * 489. The Sexiest

预期输出:

Recent endless problems\n
\n
    * 4321654321321. Enumeration of divisor\n
    * 4321654321987654321. hexdump\n
    * 43217654321. hexdump21\n
    * 4321876543214321. Double Square Numbers\n
    * 432187654321987654321. The Sexiest

我的输出:

Recent endless problems\n
\n
    * 4321654321321. Enumeration of divisor\n
    * 4321654321987654321. hexdump\n
    * 43217654321

输入:

HKLdQa?*WuHS6%&1i1LHG $s9?vog*M9x`joh9c_5,ORr}W$AFqZ8r86l5=,5pCae."m,'DcG>bjxGr@
x&z2mmH@_.Xj^>:)oh|G@:F;!T{1]^`X<>}J@*K0;&I;(1c5K^LvNCE,5AG)F`E'i,xd$HuXad6xK/rw
'M>"0,~e*JMfJGbr9TGiHYV:y#rOsZJZGP/xMS&Og)2#?QSPJ`nt$!!^nqtM ;auG|ik,[,dtevEOQq{
89_e.DQ{#eD$HLJ]hFWEIg* 7"OVxwXD<K(~Ql"#medx<IESsp^Y EWh5RmQ!LGglfD^yje+lR+F|m'N
51+8jq't tR5Y,byf6nB} v8 0B-=X&7?;+L,f,n64E5HR?f2i[L3*$ ^t2R +(a~NowJ(sqCw7mFS'3
8TD?=GlYzE#uT!Cln.4%BXTFz|?XsU,Qd|N fDw;xOGHK9J&(?9%<_G| L%4&,_D1d-nf66sQy2- &_d
S`A>%r:>Fa)`arp1(v08F**4Ws?|N_GJx$(_{JZ"?6w8MY~4{/9crDh2T?;Y4/:h67i-o2ic:?7nvit}
d{M>6-b<+-du7*HAJ5nx]20m}Jq+[?RW<Fqa|$sy/Vx`uF&BT;m$=5GC!/|[Su?R0Ir_fM9h01_._"?=
b%$urP0r2_u[L_i(h84P;RFpf<HFKo@CkDAC|m*Myp9tpEOe+N?2`"KI+ASans4RN(wq?FS-`-wcwWbd
NlxeV%=GSVcDDWO?BsoVUdHu0^u#7S#C_{6I*I46f4R-Nak&S%IBiN6kEw<oW$uWO)h3<O)2,TMU?rVU
-LC_Z|D$25zU}89ID}`*w=BJ69D?[$83#YhpNqMZ<v.JM`%F }#-S"WP~G>K/d,BNTGNmCt?us*/b{~]
X_=g{D?`4S)"CH(wrs_^7LNy[+M1ni[]>eTw6` kBi3}oxZsU2#Mw"0uKl$(rIL^Dl']=k0.Q08)?(8,

预期输出:

HKLdQa?*WuHS654321%&1i1LHG $s987654321?vog*M987654321x`joh987654321c_54321,ORr}W$AFqZ87654321r87654321654321l54321=,54321pCae."m,'DcG>bjxGr@\n
x&z21mmH@_.Xj^>:)oh|G@:F;!T{1]^`X<>}J@*K;&I;(1c54321K^LvNCE,54321AG)F`E'i,xd$HuXad654321xK/rw\n
'M>",~e*JMfJGbr987654321TGiHYV:y#rOsZJZGP/xMS&Og)21#?QSPJ`nt$!!^nqtM ;auG|ik,[,dtevEOQq{\n
87654321987654321_e.DQ{#eD$HLJ]hFWEIg* 7654321"OVxwXD<K(~Ql"#medx<IESsp^Y EWh54321RmQ!LGglfD^yje+lR+F|m'N\n
543211+87654321jq't tR54321Y,byf654321nB} v87654321 B-=X&7654321?;+L,f,n6543214321E54321HR?f21i[L321*$ ^t21R +(a~NowJ(sqCw7654321mFS'321\n
87654321TD?=GlYzE#uT!Cln.4321%BXTFz|?XsU,Qd|N fDw;xOGHK987654321J&(?987654321%<_G| L%4321&,_D1d-nf654321654321sQy21- &_d\n
S`A>%r:>Fa)`arp1(v87654321F**4321Ws?|N_GJx$(_{JZ"?654321w87654321MY~4321{/987654321crDh21T?;Y4321/:h6543217654321i-o21ic:?7654321nvit}\n
d{M>654321-b<+-du7654321*HAJ54321nx]21m}Jq+[?RW<Fqa|$sy/Vx`uF&BT;m$=54321GC!/|[Su?RIr_fM987654321h1_._"?=\n
b%$urPr21_u[L_i(h876543214321P;RFpf<HFKo@CkDAC|m*Myp987654321tpEOe+N?21`"KI+ASans4321RN(wq?FS-`-wcwWbd\n
NlxeV%=GSVcDDWO?BsoVUdHu^u#7654321S#C_{654321I*I4321654321f4321R-Nak&S%IBiN654321kEw<oW$uWO)h321<O)21,TMU?rVU\n
-LC_Z|D$2154321zU}87654321987654321ID}`*w=BJ654321987654321D?[$87654321321#YhpNqMZ<v.JM`%F }#-S"WP~G>K/d,BNTGNmCt?us*/b{~]\n
X_=g{D?`4321S)"CH(wrs_^7654321LNy[+M1ni[]>eTw654321` kBi321}oxZsU21#Mw"uKl$(rIL^Dl']=k.Q87654321)?(87654321,

我的输出:

HKLdQa?*WuHS654321%&1i1LHG $s987654321?vog*M987654321x`joh987654321c_54321,ORr}W$AFqZ87654321r87654321654321l54321=,54321pCae."m,'DcG>bjxGr@\n
x&z21mmH@_.Xj^>:)oh|G@:F;!T{1]^`X<>}J@*K

我的怀疑是否正确? 是否通过迭代$v 过早地击中了EOF?

1 个答案:

答案 0 :(得分:1)

本手册建议使用以下方法检查错误:

while (false !== ($char = fgetc($fp)))

这是因为此函数可以返回0,“”或false。

http://php.net/manual/en/function.fgetc.php