Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dealing with stupid binary vs. text mode.

Status
Not open for further replies.

shagymoe

Programmer
Apr 20, 2001
72
US
Ok, this is driving me nuts. All I want to do is open a file, search for a string, insert a string immediately after the search string and close the file. I am using fopen()fread() preg_match and preg_replace to open the original file, read it all into a string and then replace the search string with the search string plus the new string, then fopen() fwrite() and fclose() to create a new file by outputting the new string.

The problem is that when I use fopen in 'b' mode everything is fine except for the string that gets added during preg_replace doesn't format correctly. (I get the weird squares instead of newlines on windows machines.) If I use 't' mode for windows, everything is screwed up EXCEPT for the new string. WTF??? Man I miss Perl. Everything outputs to the screen correctly, but the file has this "text" and "binary" issue which I can't seem to get around. I want the script to be cross-platform, but this problem in windows is driving me up the wall. Anyone got any advice?
 
Hmmmm...well aparently you have to use \r\n in windows instead of \n. This really makes it a PIA to keep your script cross-platform. Now I have a $newline variable which I am passing to every function instead of using GLOBAL. Sheesh.

i.e.
Code:
if ($os_type == 'windows') {
    $newline="\r\n";
} else {
    $newline="\n";
}
 
Trust me, I'd love to ditch M$, but it ain't an option. Also, with Perl it isn't a problem...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top