Forum Moderators: coopster

Message Too Old, No Replies

fwrite automatically appends a newline?

         

Scooter24

1:32 pm on Sep 23, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



It seems like when you write the elements of an array to a file with fwrite, fwrite automatically appends a newline charachter (chr(10)) if the element is a string, but does not do so if the element is a number.

Is this indeed the case? The array was generated with the file function, and I used str_replace to remove all occurrences of chr(10) and chr(13).

jatar_k

4:13 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It tends to reason that if you write one line then another line it may do that but I couldn't find any concrete documentation of this behaviour.

Maybe this bump will get a few more eyes on this.

andreasfriedrich

4:47 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot reproduce the mentioned behaviour.

How do you write an array to a file with fopen, when fopen will only accept a string as the second argument?

I used the following script to test fwrite:

$a = array('Aaron','Carter');
$x = fopen("upload_dir/testfile.txt", "w");
fwrite($x, implode('',$a));
fclose($x);

produces a file whose content looks like

AaronCarter

There is no newline anywhere.

jatar_k

5:08 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



even writing one line at a time doesn't do it.

$a = array('Aaron','Carter');
$x = fopen("testfile.txt", "w");
for ($i=1;$i<=count($a);$i++)
fwrite($x, $a[$i-1]);
fclose($x);

gives

AaronCarter

Nick_W

5:18 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Surely the newline chars must be in the array elements he's writing?

<added>I know I'll regret asking this but, who's Aaron Carter?</added>

Nick

jatar_k

5:24 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you don't want to know, andreas' choice made me laugh so I reused it. You can probably search it on google.

>>must be in the array

that is what I'm thinking too.

Nick_W

5:27 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Googled. Oooooh my word, what a scary little chap!

Nick

andreasfriedrich

5:32 pm on Sep 24, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oooooh my word, what a scary little chap!

Lovely description! Thatīs great Nick! :)

Scooter24

11:48 am on Sep 25, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



How do you write an array to a file with fopen, when fopen will only accept a string as the second argument?

Well, I read the file into an array with file (). Then I remove all newline characters with a str_replace over all elements of the array (removing all occurrences of chr(10) and chr(13)).

Then I write one by one all elements of the array to a new file with a for loop. In the resulting file there are newlines at the end of each array element. PHP must be adding the newlines automatically.

Scooter24

12:15 pm on Sep 25, 2002 (gmt 0)

10+ Year Member Top Contributors Of The Month



Replying to myself... Today PHP behaves differently and does not add the newline at the end of the file. Surprise, surprise.