fgets在输出文件中增加了一行


fgets adds an extra line to output file?

下面的代码输出一个格式良好的文本文件。但它也在文本文件的末尾添加了一个额外的字符——我假设是一个''r''n或字符。我试过sed"$d",但不起作用。当我在编辑器中打开输出文件(load_data_infile.txt),并将光标放在文件的最后一行之后时,它会显示我在第"15/14"行,这很奇怪。如何在一个14行的文件中包含15行?

我的问题是:1)是什么导致了第"第15行,共14行";2)我如何阻止它被写入或删除它?

谢谢!

    <?php
$file_write_output = fopen("load_data_infile.txt", "w") or die ("can't open load_data_infile.txt for writing");
$file_read_input = fopen("http://file.txt", "r") or die ("can't open file");
//perform the loop through the file - read the input file line by line with fgets function
while (!feof($file_read_input) ) {
$input = fgets($file_read_input);

echo "$input";
fwrite($file_write_output,$input);
}
fclose($file_write_output);
fclose($file_read_input);
?>
<?php
//strip out the header of load_data_infile.txt file

exec("sed -i '1d' load_data_infile.txt");

?>

试试这个:

$content = file_get_contents('http://file.txt');
if ($file !== false) {
    return $file;
}
if ( file_put_contents('load_data_infile.txt', $content, LOCK_EX) ) {
    // Success
}

PHP文档:PHP文件集内容和PHP文件_输出_内容

下面的代码正在工作!

<?php
    $raw_data = file_get_contents('http://file.txt');
    $trimmed_raw_data=trim($raw_data);
    echo $trimmed_raw_data;
    if (file_put_contents('load_data_infile.txt', $trimmed_raw_data, LOCK_EX) ) 
?>