dppxp79175 2013-07-08 19:46
浏览 219

即使文件不为空,fgets()也会返回false

I have a form from which I save the given input into a textfile,
but I have trouble reading from the saved file:

while(!feof($fileNotizen)) {
$rawLine = fgets($fileNotizen);
if($rawLine==false) {
  echo "An error occured while reading the file"; 
}


$rawLine seems to be always false, even though I use this function before, to fill the textfile:

function addToTable($notizFile) {
 fwrite($notizFile, $_POST["vorname"]." ".$_POST["nachname"]."#");
 $date = date(DATE_RFC850);
 fwrite($notizFile, $date."#");
 fwrite($notizFile, $_POST["notiz"].PHP_EOL);   
}


And after I submit the form and get the error message, if I check the textfile, everything is there, so the function works correctly.

If it is of value, I open the file with this command:

$fileNotizen = fopen("notizen.txt", "a+");

Could the problem be that the pointer is already at the end of the file and thus returns false?

  • 写回答

2条回答 默认 最新

  • douqiang5809 2013-07-08 19:49
    关注
    $fileNotizen = fopen("notizen.txt", "a+");
    

    a+ opens for read/write but places file pointer AT THE END. So you must fseek() to the beginning first or look into fopen() flags and choose more wisely based on your needs.

    Use fseek($fileNotizen, 0, SEEK_SET); to rewind the file.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来