[SOLVED] fwrite and newline chars

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
percepts
Forum Newbie
Posts: 10
Joined: Wed Sep 29, 2004 4:58 pm

[SOLVED] fwrite and newline chars

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
percepts
Forum Newbie
Posts: 10
Joined: Wed Sep 29, 2004 4:58 pm

Post by percepts »

:lol: cheers matey. the double quotes did it.
:lol:
Post Reply