0

I test this PHP code on windows and it first removes the file.txt contents and then write the new contents on it.

$f = fopen('file.txt', 'r+');
fwrite($f, "first-time");
fclose($f);

Every time I execute this code and see file.txt, it has ONE "first-time" in it.

I expect it to prepend "first-time" to the old file. like: first-timefirst-timefirst-time and so on.

Why r+ acts like w+ in making zero length?

user3449439
  • 59
  • 10
  • 3
    Could you tag this with what language it should be? And tell us in your question? And what outcome are you expecting? – Teepeemm Jul 10 '14 at 12:36
  • 4
    I pressume language you're using is php. In this case, "r+" mode places the file pointer at the beginning of the file and this is why it overwrites each time you run the code. You need to use "a" (or "a+") mode. This will place file pointer to the end of the file. Like so: `$f = fopen('file.txt', 'a')`; – Davit Jul 10 '14 at 12:46
  • 2
    -1: _Read the documentation for functions that you use._ Everything you need is there. – Lightness Races in Orbit Jul 10 '14 at 12:46
  • But @Davit I want to append that at the first, not the end. I can use file_get_contents() and file_put_contents() in order to do that but I want to see how it is possible with fopen. tnx – user3449439 Jul 10 '14 at 12:49
  • @user3449439 Please accept my answer then, if it does what you want. – tholu Jul 10 '14 at 12:51
  • It does but I knew that. My question is why r+ acts like w+ in making zero length – user3449439 Jul 10 '14 at 12:54
  • 1
    @user3449439: "Appending" means "adding to the end". What you are asking for, then, is "prepending". Did you perform any research into how to do this? – Lightness Races in Orbit Jul 10 '14 at 12:54
  • BTW you spelt "first" wrong. I've fixed your title as it accused a function of "not working" when in fact you simply failed to look up what it does. – Lightness Races in Orbit Jul 10 '14 at 12:55
  • OK sorry for my bad english. I meant prepend – user3449439 Jul 10 '14 at 12:57
  • @user3449439 Thanks for the clarification. Would be nice if you could reconsider my answer now. – tholu Jul 10 '14 at 13:00

2 Answers2

1

Answer to your question: Your code does not truncate the file, but simply overwrites the previous content.

Quick and dirty solution for the behaviour your want to achieve:

<?php
$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('file.txt');
file_put_contents('file.txt', $file_data);
?>
tholu
  • 1,150
  • 2
  • 10
  • 24
  • Read his question, he wants to append at the beginning = prepending. – tholu Jul 10 '14 at 12:49
  • it doesn't use fopen and I don't like such script because it's not neat. unfortunately I think r+ acts like w+ in making that file to zero bytes. – user3449439 Jul 10 '14 at 12:52
  • You did not specify that it has to use fopen. It is neat enough, especially if you obviously are not familiar with fopen. – tholu Jul 10 '14 at 12:54
  • @tholu: "append at the beginning" makes no sense. If he means prepending he should say prepending. – Lightness Races in Orbit Jul 10 '14 at 12:54
  • @LightnessRacesinOrbit I think he is not a native English speaker. But you see that obviously my code does what he wants. It would be fair to remove your downvote here. – tholu Jul 10 '14 at 12:56
  • According to the discription in php.net . r+ should do the work. But I don't know why I should get the contents first and then append it and after it write. :| so what doesn r+ here? – user3449439 Jul 10 '14 at 13:01
  • @user3449439 You have to understand, that when you put the pointer at the beginning, it just starts to write there. It does not move the existing data, it just overwrites it. That's why you get the contents, piece it together in memory and write the result back to the file. – tholu Jul 10 '14 at 13:03
  • 1
    Ohh. so it overwrites it. It was the answer that helped me. – user3449439 Jul 10 '14 at 13:05
0

EDIT: The answer can be found here: How do I prepend file to beginning?

According to the PHP fopen manual:

'r+' Open for reading and writing; place the file pointer at the beginning of the file.

'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

$f = fopen('file.txt', 'a+');
fwrite($f, "fisrt-time");
fclose($f);

So if you'd like to append content, use a+ mode.

Community
  • 1
  • 1
zeldi
  • 4,833
  • 3
  • 19
  • 18
  • I'm not sure what benefit there is in repeating the code in the question. You don't even write any prose to explain your answer. – Lightness Races in Orbit Jul 10 '14 at 12:47
  • Everybody should read the question, he wants to append at the beginning = prepending! – tholu Jul 10 '14 at 12:50
  • @Lightness Races in Orbit: I accidentally submited it while pressing tab and enter. I see stackoverflow doesn't show that the answer is beeing actively edited. Thanks for the downvotes anyway. – zeldi Jul 10 '14 at 12:51
  • Tricky language question. – zeldi Jul 10 '14 at 12:56
  • @zeldi Please see my answer - I guess you downvoted that, too. – tholu Jul 10 '14 at 12:59
  • 1
    @tholu: I haven't woted anybody's answer. I guess it's just an active/hot question with tricky word phrasing (append at the beggining), getting tons of editors. Your solution works, albeit it can be tricky with large files. – zeldi Jul 10 '14 at 13:04