Page 1 of 1

[SOLVED] fwrite and newline chars

Posted: Tue Apr 12, 2005 8:00 am
by percepts
I have been trying to write a little bit of simple code to output a 2 line file with some exchange rates in it.
Problem is that even though I have put \n for new line characters, when I read the file back it always comes back as one line.

I have tried using fgets and fgetss to read it back and even though the PHP manual says length is not required for fgets it gives and a warning and then displays both writes as one line.


Code: Select all

$filename = &quote;currencies.txt&quote;;
		$fp2 = fopen( $filename, &quote;w&quote; );
		$rec = 'USD='.$usd.'\n';
		fwrite( $fp2, $rec, strlen($rec));
		$rec =  'EUR='.$eur.'\n';
		fwrite( $fp2, $rec, strlen($rec));
		fclose( $fp2 );
and then to read the file

Code: Select all

$filename = &quote;currencies.txt&quote;;
		$fp3 = fopen( $filename, &quote;r&quote; );
		$rec = fgets($fp3);
		echo '<br />currencies<br />';
		echo $rec;
		fclose( $fp3 );
the \n charaters are embedded in the output but they don't cause the the fgets to read one line at a time. What is wrong?

looking at the created file shows both rows on one line like this:

USD=1.2985\nEUR=0.68540\n

Why?

tia

Posted: Tue Apr 12, 2005 8:12 am
by Chris Corbyn
Are you using windows?

in which case you need

Code: Select all

\r\n
also you need double quotes for these parts.... single quotes are too literal

Posted: Tue Apr 12, 2005 8:23 am
by percepts
:lol: cheers matey. the double quotes did it.
:lol: