Put pointer on second line to write?

Asked

Viewed 1,226 times

0

Writing a text in the file

    $fn = fopen('teste.txt', 'a');
    fwrite($fn, '-meu texto aqui');
    fclose($fn);

Reading the second line of the file

    $fn = "teste.txt";
    $nr_linha = 1;
    $f_contents = file ($fn);
    @$sua_linha = $f_contents [$nr_linha];
    print $sua_linha;

How can I modify the first part of the code to record only on the second line? I tried it unsuccessfully!

    $fn = fopen('teste.txt', 'a');
    @$fc = file ($fn);
    @$fn = $fc [1];
    fwrite($fn, '-alteração na segunda linha');
    fclose($fn);
  • You know the size of the first line or it is given perlo line terminator?

  • The size of the first line is fixed only two letters being them "<?".

  • I realized that in this case will be irrelevant already the others may vary, so it is better to write the whole file again.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

2 answers

2

The first mistake is to use the append. You want to write in the archive and not add something to the end of the file.

The other thing is that you need to find where the first line ends and start writing right after. There are several ways to do this. One of them that is simpler is to write the first line again after reading it. I think you tried to do but actually threw the first line contents on top of the file handler, ie you lost the connection to the file.

Actually, there’s another problem. The second line may be larger or smaller than the original content, so it would be better to write again everything that comes after.

$fn = fopen('teste.txt', 'w'); //note que isto destrói o conteúdo do arquivo
$fc = file($fn);
$fc[1] = '-alteração na segunda linha';
file_put_contents($fn, $fc);
fclose($fn);

I put in the Github for future reference.

I kept the names of the variables so as not to confuse you but I don’t know if it’s just them that confused you. Giving meaningful names to variables help a lot to understand the code.

Finally do not try to access the file concurrently since there is no locking control on this algorithm.

0

Just do it that way:

<?php

//Lê o arquivo
$linhas = explode("\n", file_get_contents("./arquivo.txt"));

//Lê somente o conteúdo da linha [1] do array ou seja linha 2 do texto
$linha_n = $linhas[1];

//Abre o arquivo colocando o ponteiro de escrita no final
$arquivo = fopen('arquivo.txt','r+');
      if ($arquivo) {
while(true) {
$linha = fgets($arquivo);
if ($linha==null) break;

//Busca o conteúdo que vai ser alterado
if(preg_match("/$linha_n/", $linha)) {
//este é o lugar onde vai trocar a palavra
$string .= str_replace("$linha_n", "novo texto para linha", $linha);
} else {
//Vai colocando tudo numa nova string
$string.= $linha;
}
}
//Move o ponteiro para o início
rewind($arquivo);

//Apaga todo o conteúdo
ftruncate($arquivo, 0);

//Reescreve conteúdo do arquivo
if (!fwrite($arquivo, $string)) die('Não foi possível atualizar');
echo 'Arquivo atualizado';
fclose($arquivo);
}
?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.