Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php fgets()返回空字符串_Php_String - Fatal编程技术网

Php fgets()返回空字符串

Php fgets()返回空字符串,php,string,Php,String,我试图读取文本文件中的第一行,但fgets()函数似乎无法完成此任务。我有一种预感,这可能是由于新行在字符串中的处理方式,但我对新行的不同表示方式不是很有经验。我有一个网站的图片,上面显示了新行输入的内容以及我的代码 链接(图像无代表): 我已经尝试将模式从w改为w+,我认为这可以解决问题,但没有。我还尝试在php中使用trim()函数修剪字符串,但也不起作用。我已经确认我实际上也在写入文件 这是密码 <?php $input = "def sum(numbers): to

我试图读取文本文件中的第一行,但fgets()函数似乎无法完成此任务。我有一种预感,这可能是由于新行在字符串中的处理方式,但我对新行的不同表示方式不是很有经验。我有一个网站的图片,上面显示了新行输入的内容以及我的代码

链接(图像无代表):

我已经尝试将模式从w改为w+,我认为这可以解决问题,但没有。我还尝试在php中使用trim()函数修剪字符串,但也不起作用。我已经确认我实际上也在写入文件

这是密码

<?php
    $input = "def sum(numbers):
    total = 0
    for x in numbers:
        total += x
    return total
print(sum((8, 2, 3, 0, 7)))";

    $answerFile = fopen("/afs/cad.njit.edu/u/a/j/ajr74/public_html/answer.txt", "w+") or die("Unable to open file.");

    fwrite($answerFile, $input);
    $line = fgets($answerFile);

    print($line);

?>

我希望输出是第一个CR LF标记之前的文件的第一行,但我得到的是一个空输出。

很有趣

执行写操作后,需要重置指针以读取回内容。最好的方法是关闭它,然后打开阅读

<?php
$input = "def sum(numbers):
    total = 0
    for x in numbers:
        total += x
    return total
print(sum((8, 2, 3, 0, 7)))";

//$file_name = "/afs/cad.njit.edu/u/a/j/ajr74/public_html/answer.txt";
$file_name = "./answer.txt";
echo "Write {$file_name} and Read back a line<br>";

$answerFileHandle = fopen($file_name, "w+") or die("Unable to open file.");
fwrite($answerFileHandle, $input);

// Required these two lines to close and re-open the file.
// Resets the pointer after the write operation above
fclose($answerFileHandle);
$answerFileHandle = fopen($file_name, "r") or die("Unable to open file.");

$line = fgets($answerFileHandle);
echo $line;
fclose($answerFileHandle);
有趣

执行写操作后,需要重置指针以读取回内容。最好的方法是关闭它,然后打开阅读

<?php
$input = "def sum(numbers):
    total = 0
    for x in numbers:
        total += x
    return total
print(sum((8, 2, 3, 0, 7)))";

//$file_name = "/afs/cad.njit.edu/u/a/j/ajr74/public_html/answer.txt";
$file_name = "./answer.txt";
echo "Write {$file_name} and Read back a line<br>";

$answerFileHandle = fopen($file_name, "w+") or die("Unable to open file.");
fwrite($answerFileHandle, $input);

// Required these two lines to close and re-open the file.
// Resets the pointer after the write operation above
fclose($answerFileHandle);
$answerFileHandle = fopen($file_name, "r") or die("Unable to open file.");

$line = fgets($answerFileHandle);
echo $line;
fclose($answerFileHandle);

我猜文件指针在末尾。要将其恢复,您可以在写入文件和读取文件之间使用

所以

变成

    fwrite($answerFile, $input);
    rewind($answerFile);
    $line = fgets($answerFile);

注意:除了你已经有了内容之外。通常,您不需要从文件中读取表单,只需从
$input
中提取第一行即可。但是我想你可能只是在学习或者计划在两个不同的点上做这件事。

我想文件指针在末尾。要将其恢复,您可以在写入文件和读取文件之间使用

所以

变成

    fwrite($answerFile, $input);
    rewind($answerFile);
    $line = fgets($answerFile);

注意:除了你已经有了内容之外。通常,您不需要从文件中读取表单,只需从
$input
中提取第一行即可。但我想您可能只是在学习或计划从两个不同的角度来做这件事。

在文本编辑器中直接查看文件时,您会看到什么?它是否存在?是的,当我查看文件answer.txt时,它包含字符串。您是否只想读回一行,即def sum(数字):?在文本编辑器中直接查看文件时,您会看到什么?它存在吗?是的,当我查看文件answer.txt时,它包含字符串。您是否只想读回一行,即def sum(数字):?这就是我缺少的:)ole回放。尝试从输入中提取第一行对我不起作用,所以我尝试将其放入文件中。@Leonidous感谢您接受。。另一方面,如果您仍然希望在不从文件中读取第一行的情况下获取第一行,您可以尝试使用一些简单的方法,如
$array=explode(PHP_EOL,$input)
,这将为您获取一个包含
$input
中的行的数组,然后您将在第一个元素上找到所需的内容,所以在
$array[0]
这就是我所缺少的:)ole倒带。尝试从输入中提取第一行对我不起作用,所以我尝试将其放入一个文件中。@Leonidous感谢您接受。。另一方面,如果您仍然希望在不从文件中读取第一行的情况下获取第一行,您可以尝试使用一些简单的方法,如
$array=explode(PHP_EOL,$input)
,这将为您获取一个包含
$input
中的行的数组,然后您将在第一个元素上找到所需的内容,因此,
$array[0]