Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.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 fwrite()复制并替换通过fgets()获得的行_Php_Fwrite_Fgets - Fatal编程技术网

PHP fwrite()复制并替换通过fgets()获得的行

PHP fwrite()复制并替换通过fgets()获得的行,php,fwrite,fgets,Php,Fwrite,Fgets,我正在尝试编写一个小php函数,它每隔10行读取一次,检测是否有分号,并用commit语句替换它进入承诺 我使用fgets是因为该文件可能非常大,比如1GB,而我不想将其保存到另一个新文件中,这样会占用更多空间 我的职能是 $targetFile=fopen($file,'r+') 执行后: LINE1; LINE2; LINE3; LINE4; LINE5; LINE6; LINE7; LINE8; LINE9; LINE10; LINE10; commit; LINE13; 问题是为什么f

我正在尝试编写一个小php函数,它每隔10行读取一次,检测是否有分号,并用commit语句替换它进入承诺


我使用fgets是因为该文件可能非常大,比如1GB,而我不想将其保存到另一个新文件中,这样会占用更多空间

我的职能是

$targetFile=fopen($file,'r+')

执行后:

LINE1;
LINE2;
LINE3;
LINE4;
LINE5;
LINE6;
LINE7;
LINE8;
LINE9;
LINE10;
LINE10; commit;
LINE13;

问题是为什么fwrite()会替换第11行并删除第12行

问题在于,当您使用
在(($lines=fgets($targetFile))!==false)
时,文件指针向前移动。所以当你到达第10行时,你的文件指针已经指向下一行,第11行,所以你覆盖了第11行

这就是我要做的:

$filename = "input.txt";
$in = fopen($filename,'r+');
if($in)
{
  $count = 0;
  $last = 0;

  // loop every line
  while (($lines = fgets($in)) !== false)
  {
    $count++;
    if( $count % 10 == 0 && preg_match('/;/',$lines) )
    {
      $content = file_get_contents($filename, FALSE, null, ftell($in));

      $insertLine = preg_replace('/;/',"; commit;", $lines);

      // go back to the end of the last line
      fseek($in, $last, SEEK_SET);
      fwrite($in, $insertLine);

      // insert the content from the next line to the end, otherwise it will modified. You see this if you remove this line 
      fwrite($in, $content);
    }
    else
    {
      //Save the position where ends the last line
      $last = ftell($in);
    }
  }
}
else
{
  echo "Input file cannot be opened\n";
}

fclose($in);

您可以找到一种更有效的方法来获取文件的内容
$content=file\u get\u contents($filename,FALSE,null,ftell($in))或根据需要使用临时文件。

问题是,当您使用($lines=fgets($targetFile))!==false)时($lines=fgets($targetFile))
迭代文件时,文件指针向前移动。所以当你到达第10行时,你的文件指针已经指向下一行,第11行,所以你覆盖了第11行

这就是我要做的:

$filename = "input.txt";
$in = fopen($filename,'r+');
if($in)
{
  $count = 0;
  $last = 0;

  // loop every line
  while (($lines = fgets($in)) !== false)
  {
    $count++;
    if( $count % 10 == 0 && preg_match('/;/',$lines) )
    {
      $content = file_get_contents($filename, FALSE, null, ftell($in));

      $insertLine = preg_replace('/;/',"; commit;", $lines);

      // go back to the end of the last line
      fseek($in, $last, SEEK_SET);
      fwrite($in, $insertLine);

      // insert the content from the next line to the end, otherwise it will modified. You see this if you remove this line 
      fwrite($in, $content);
    }
    else
    {
      //Save the position where ends the last line
      $last = ftell($in);
    }
  }
}
else
{
  echo "Input file cannot be opened\n";
}

fclose($in);

您可以找到一种更有效的方法来获取文件的内容
$content=file\u get\u contents($filename,FALSE,null,ftell($in))或者根据您的需要使用临时文件。

对于1 GB的文件来说,这简直是谋杀。唯一的解决办法是写一个新的临时文件。是的,你是对的,最好的方法是一个临时文件。我没有进一步调查,因为问题是“为什么fwrite()会替换第11行并删除第12行”,这对于1GB文件来说是谋杀。唯一的解决办法是写一个新的临时文件。是的,你是对的,最好的方法是一个临时文件。我没有进一步调查,因为问题是“为什么fwrite()会替换第11行并删除第12行”“我不想将其保存到另一个新文件中”-很难,您必须这样做。I/O不是基于线路的。不能替换行,只能替换字节。也就是说,当且仅当新行的行长完全相同时,才可以替换一行—如果新行较长(或较短),则必须编写一个新文件。明白。因此,这不是一个合理的解决方案。我们可以为每n行添加一个单词或一行吗?假设空间很贵,保存到一个新文件中不是我们最好的选择吗?空间几乎总是你最便宜的资源,但是OK。您可以做的是保留一个滚动缓冲区来存储多余的数据。该缓冲区将随着您添加到文件中的每一个额外字节而增长,因此,只有当您知道添加的总内存小于您想要使用的内存时,该方法才有用。理想情况下,使用基于记录的存储,例如SQL。“我不想将其保存到另一个新文件中”-很难,你必须这样做。I/O不是基于线路的。不能替换行,只能替换字节。也就是说,当且仅当新行的行长完全相同时,才可以替换一行—如果新行较长(或较短),则必须编写一个新文件。明白。因此,这不是一个合理的解决方案。我们可以为每n行添加一个单词或一行吗?假设空间很贵,保存到一个新文件中不是我们最好的选择吗?空间几乎总是你最便宜的资源,但是OK。您可以做的是保留一个滚动缓冲区来存储多余的数据。该缓冲区将随着您添加到文件中的每一个额外字节而增长,因此,只有当您知道添加的总内存小于您想要使用的内存时,该方法才有用。理想情况下,使用基于记录的存储,例如SQL。
$filename = "input.txt";
$in = fopen($filename,'r+');
if($in)
{
  $count = 0;
  $last = 0;

  // loop every line
  while (($lines = fgets($in)) !== false)
  {
    $count++;
    if( $count % 10 == 0 && preg_match('/;/',$lines) )
    {
      $content = file_get_contents($filename, FALSE, null, ftell($in));

      $insertLine = preg_replace('/;/',"; commit;", $lines);

      // go back to the end of the last line
      fseek($in, $last, SEEK_SET);
      fwrite($in, $insertLine);

      // insert the content from the next line to the end, otherwise it will modified. You see this if you remove this line 
      fwrite($in, $content);
    }
    else
    {
      //Save the position where ends the last line
      $last = ftell($in);
    }
  }
}
else
{
  echo "Input file cannot be opened\n";
}

fclose($in);