PHP阅读fscanf

问题描述 投票:-1回答:2

我一直在尝试从给定输入作为STDIN的在线课程中读取数字,我的文件是这样的:

2
3 15

我读了2和3,怎么读15?

我的代码:

    <?php
    /*
    // Sample code to perform I/O:

    fscanf(STDIN, "%s\n", $name);           // Reading input from STDIN
    echo "Hi, ".$name.".\n";                // Writing output to STDOUT

    // Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
    */

    // Write your code here
    fscanf(STDIN, "%s\n", $ammount);
    echo $ammount . "\n";
    for ($i = 0; $i <= $ammount; $i++)
{
    fscanf(STDIN, "%s", $x);
    echo $x . "\n";
}
    ?>

我现在的输出是:2 3 3我想要2 3 15

php scanf
2个回答
0
投票

fscanf在线上工作,但%s默认停止阅读太空。使用:

fscanf(STDIN, "%[^\n]", $x);

并用空格爆炸$x


0
投票

我试图推动你的是在你的格式字符串中使用一个空格并将你的输入读入单独的变量,就像这样。

fscanf(STDIN, "%s %s", $a, $b);
echo $a . ' ' . $b . "\n";
© www.soinside.com 2019 - 2024. All rights reserved.