By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Write to binary file

P: n/a
Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

?>
Dec 8 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmail.comwrote:
Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

?>
Nothing wrong with the code, what's wrong with the result?
--
Rik Wasmus
Dec 8 '07 #2

P: n/a
zach wrote:
Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);
Is that a game like spot the difference?

Thing I notice straight away is the lack of closing php tag.
Dec 8 '07 #3

P: n/a
Rik Wasmus wrote:
On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmail.comwrote:
>Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

?>

Nothing wrong with the code, what's wrong with the result?
Maybe nothing, I just thought that by writing text in binary mode it
would make it unreadable (for the most part). No matter what I write or
how much its still just plain ascii text. When I write text in C it
pretty much unreadable.
Dec 8 '07 #4

P: n/a
zach wrote:
$ptr = fopen($fileName, 'wb');
Windows or Linux host?

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Me cansé, no escribo más Tag's ... 8-(
Dec 8 '07 #5

P: n/a
Paul Lautman wrote:
zach wrote:
>Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

Is that a game like spot the difference?

Thing I notice straight away is the lack of closing php tag.

That was a copy paste error I have it in my script.
Dec 8 '07 #6

P: n/a
Iván Sánchez Ortega wrote:
zach wrote:
>$ptr = fopen($fileName, 'wb');

Windows or Linux host?
Windows, as I replied to Rik, it might be working correctly and I'm just
expecting something different. The result is it creates the file, and
writes plain ascii text and I thought it would be unreadable.
Dec 8 '07 #7

P: n/a
On Sat, 08 Dec 2007 22:06:18 +0100, Paul Lautman
<pa**********@btinternet.comwrote:
zach wrote:
>Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

Is that a game like spot the difference?

Thing I notice straight away is the lack of closing php tag.
Newsreader error, the ?is actually there.
--
Rik Wasmus
Dec 8 '07 #8

P: n/a
On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmail.comwrote:
Rik Wasmus wrote:
>On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmail.comwrote:
>>Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

?>
Nothing wrong with the code, what's wrong with the result?

Maybe nothing, I just thought that by writing text in binary mode it
would make it unreadable (for the most part).
You misunderstand binary I think. It just means no newline 'translation'
takes place.
No matter what I write or how much its still just plain ascii text. When
I write text in C it pretty much unreadable.
The string is here is ascii text (well, it might have characters outside
ascii depending on the string the particular character encoding used). So
in the file will be what you told $string is. Checking a hex editor, the
contents of your file should prove to be exactly the same bytes as the
string in your script. In C data types are handled quite more complex
(well, under water this is also true for PHP, but you needn't concern
yourself with it while writing in PHP), possibly generating a different
representation when outputting the raw data.

What is the problem/issue you are actually facing that you want this
'binary' writing?
--
Rik Wasmus
Dec 8 '07 #9

P: n/a
Rik Wasmus wrote:
On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmail.comwrote:
>Rik Wasmus wrote:
>>On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmail.comwrote:

Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

?>
Nothing wrong with the code, what's wrong with the result?

Maybe nothing, I just thought that by writing text in binary mode it
would make it unreadable (for the most part).

You misunderstand binary I think. It just means no newline 'translation'
takes place.
>No matter what I write or how much its still just plain ascii text.
When I write text in C it pretty much unreadable.

The string is here is ascii text (well, it might have characters outside
ascii depending on the string the particular character encoding used).
So in the file will be what you told $string is. Checking a hex editor,
the contents of your file should prove to be exactly the same bytes as
the string in your script. In C data types are handled quite more
complex (well, under water this is also true for PHP, but you needn't
concern yourself with it while writing in PHP), possibly generating a
different representation when outputting the raw data.

What is the problem/issue you are actually facing that you want this
'binary' writing?
Well its nice to know I was writing my code right, lol. I was expecting
it to look different, like binary writing.
Dec 8 '07 #10

P: n/a
Rik Wasmus wrote:
On Sat, 08 Dec 2007 22:06:18 +0100, Paul Lautman
<pa**********@btinternet.comwrote:
>zach wrote:
>>Can someone help me out, I can't figure out what I'm doing wrong to
write to a file in binary mode.

What's wrong with my code?

<?php

$fileName = "something.dat";
$string = "This is a string of text";

$ptr = fopen($fileName, 'wb');

fwrite($ptr, $string);
fclose($ptr);

Is that a game like spot the difference?

Thing I notice straight away is the lack of closing php tag.

Newsreader error, the ?is actually there.
OE Quotefix is doing it. But when someone posts such an open question, it's
like a game trying to spot anything that could possibly be wrong.
Dec 8 '07 #11

P: n/a
zach wrote:
Rik Wasmus wrote:
>On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmail.comwrote:
>>Rik Wasmus wrote:
Well its nice to know I was writing my code right, lol. I was
expecting it to look different, like binary writing.
Maybe next time you have a question like this, you could actually tell us
the problem that you think you are facing, rather than have everone try to
guess?
Dec 8 '07 #12

P: n/a
Paul Lautman wrote:
zach wrote:
>Rik Wasmus wrote:
>>On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmail.comwrote:

Rik Wasmus wrote:
Well its nice to know I was writing my code right, lol. I was
expecting it to look different, like binary writing.
Maybe next time you have a question like this, you could actually tell us
the problem that you think you are facing, rather than have everone try to
guess?

Thanks, I'll try to be more specific in the future. But I'll also
suggest you look at Rik's response and learn from him.
Dec 8 '07 #13

P: n/a
zach wrote:
Paul Lautman wrote:
>zach wrote:
>>Rik Wasmus wrote:
On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmail.com>
wrote:
Rik Wasmus wrote:
Well its nice to know I was writing my code right, lol. I was
expecting it to look different, like binary writing.
Maybe next time you have a question like this, you could actually
tell us the problem that you think you are facing, rather than have
everone try to guess?

Thanks, I'll try to be more specific in the future. But I'll also
suggest you look at Rik's response and learn from him.
Which of Rik's responses and what is it I'm supposed to be learning?
Dec 9 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.