3

I'm just start learning php and have a question about feof(). I wanna read 2 strings from txt file and put it into two variables. But something going wrong, because var_dump show me FALSE.

$fbhand = fopen("c://study/file.txt","r");
while (!feof($fbhand)) {
    $fizz = fgets($fbhand);
    $buzz = fgets($fbhand);
}

Before that i created this file and put there 2 numbers from STDIN and W+, than closed file.

function dataEntry() {
    $fbfile = ("c://study/file.txt","w+");
    $handler = fopen("php://stdin", "r");
    echo "enter f \n";
    $f = fgets($handler);
    fwrite($fbfile, $f);
    echo "enter b\n";
    $b = fgets($handler);
    fwrite($fbfile,$b);
    fclose($fbfile);
}

what i'm doing wrong?

  • 3
    https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong and please check that the file is opened – Ed Heal Feb 04 '18 at 23:28
  • `c://study/file.txt` looks like an URL but there is no `c` protocol. I cannot tell for sure but I suspect `fopen()` fails because of that. What you probably mean is `c:/study/file.txt`. – axiac Feb 04 '18 at 23:37
  • 1
    Your code isn't even valid PHP. you can't run it without a syntax error. Also, you have to fopen $fbfile before you can fwrite to it. – S. Imp Feb 04 '18 at 23:47
  • "var_dump show me FALSE" - `var_dump()` of ***what***? – Mawg says reinstate Monica Feb 06 '18 at 10:04
  • 1
    @Mawg if i vardump `$fizz` or `$buzz` it show null. feof runs like endless cicle, intersting why it's happened – texas_panda Feb 06 '18 at 13:29
  • 1
    As @EdHeal said, you are almost certainly looping for ever on `while (!feof($fbhand))` because you cannot detect `feof(NULL)`, which is what will haven if `$handler = fopen("php://stdin", "r");` fails - check if `$handler == NULL` – Mawg says reinstate Monica Feb 06 '18 at 13:34
  • @Mawg ouch! thx, so stupid fail! – texas_panda Feb 06 '18 at 14:38

0 Answers0