473,625 Members | 3,357 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Write to binary file

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
13 18178
On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmai l.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
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
Rik Wasmus wrote:
On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmai l.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
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
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
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
On Sat, 08 Dec 2007 22:06:18 +0100, Paul Lautman
<pa**********@b tinternet.comwr ote:
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
On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmai l.comwrote:
Rik Wasmus wrote:
>On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmai l.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
Rik Wasmus wrote:
On Sat, 08 Dec 2007 22:07:02 +0100, zach <wa*******@gmai l.comwrote:
>Rik Wasmus wrote:
>>On Sat, 08 Dec 2007 21:47:13 +0100, zach <wa*******@gmai l.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($pt r, $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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
15662
by: kee | last post by:
Hi All, I am trying to write binary data to a file, which is bmp image: Open "d:\temp\test001.bmp" For Binary Access Write As #1 Put #1, 1, strImage Close #1 *** strImage contains binary data
4
3481
by: Jon Hyland | last post by:
Hi all, I'm looking for the fastest way to write (and/or read) binary to a file in VC++. I've been using the fstream object in this way: //unsigned char *pDataOut and long iLength initilized somewhere.. fstream file_out((const char *)pszOutFile, ios::in|ios::trunc|ios::binary);
1
1867
by: sleepylight | last post by:
I'm running into a strange problem with ostreams's write function. Please see the code below: ofstream save((job->b->get_filename()).c_str(), ios::out | ios::binary); if (!save) { cout << "Cannot open file" << endl; return; } else {
5
3652
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes written in file are not the sent bytes. Copy and paste the following lines to observe my problem. What can I do to resolve problem ? Only System.Text.Encoding.ASCII write the same number of bytes, but not the good bytes. Someone can help me. Thanks by...
3
8523
by: Billy Smith | last post by:
I'm trying to write a little utility that will write some binary data to a file via a javascript and Windows Script Host under Windows XP. The only way to do this that I can find is to convert the binary data to text via String.fromCharCode() function and then write to the file with TextStream.Write(). But that function gives an "invalid parameter" error message when I try to write some ASCII codes to the file. I could understand if...
6
7364
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a chunk of the file into a buffer, when it encounters the end of the file, will the EOF indicator be put into the buffer automatically just as an ordinary byte of the file, or do I have to do it manually?
2
3540
by: Thomi Aurel RUAG A | last post by:
Hy I'm using Python 2.4.2 on an ARM (PXA-270) platform (linux-2.6.17). My Goal is to write a list of bytes down to a file (opened in binary mode) in one cycle. The crux is that a '0x0a' (line feed) will break the cycle of one writing into several pieces. Writing to a "simple" file, this wouldn't cause any problem. Assuming - without questioning ;-) - that a device file (/dev/*) has to be written in one cycle because one write call will...
6
5233
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs = File.Open(@"D:\test.dat", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs);
1
3897
by: =?Utf-8?B?U3RldmVU?= | last post by:
I have a structure that contains both 32x32 and 16x16 icons plus some text. I want to write all this to an XML file so that I can recover the icons later in an application. Can someone tell me how to properly serialize the System.Drawing.Icon structure to an XML file? The following code doesn't write the icon information to the xml file. private void CreateXmlFile(string filename) {
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7184
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6118
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.