473,385 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

write a binary file?

Dear all,

I open a binary file and want to write 0x00040700 to this file.
how can I set write buffer?
---------------------------------------------------
typedef unsigned char UCHAR;
int iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRO NLY,S_IREAD|S_IWRITE);
UCHAR buffer[5]; //???????????
write(iFD,buffer,5);
---------------------------------------------------

Thanks.

Regards,
cylin.
Nov 14 '05 #1
20 5545
"cylin" <cy***@avant.com.tw> wrote:
I open a binary file and want to write 0x00040700 to this file.
how can I set write buffer?
Not like this, in ISO C. What you've got is system-specific, either
POSIX or not-quite-POSIX-M$.
typedef unsigned char UCHAR;
Yeugh.
int iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRO NLY,S_IREAD|S_IWRITE);
Hungarian notation. Double yeugh. HN used to declare what everybody
reading your code, including the compiler, already knows: the types of
your identifiers. Triple yeugh, and go to the self-confidence shop and
buy some.
UCHAR buffer[5]; //???????????
Mixing declarations and executable statements is legal in C99 and C++,
but not in C89. Beware the snark.
write(iFD,buffer,5);


No idea how to solve this using your low-level, unlikely-to-port
functions. In _real_ C, you'd do something like this:

#include <stdio.h>

unsigned char buf[5];
FILE *outfile;

if (!(outfile=fopen(filename, "wb")) {
puts("This is the place where you'd handle a file open error.");
} else {
if (fwrite(buf, sizeof *buf, sizeof buf/sizeof *buf, outfile) !=
sizeof buf/sizeof *buf) {
puts("Handle a write error here.");
}
/* Or, since you know that buf is an unsigned char array and
therefore sizeof *buf must be 1:
fwrite(buffer, 1, sizeof buf, outfile);
*/
fclose(outfile);
/* For critical applications, you should even check the return value
of fclose(), but I rarely do this. */
}

Issa dat si'ple.

Richard
Nov 14 '05 #2
"cylin" <cy***@avant.com.tw> wrote:

I open a binary file and want to write 0x00040700 to this file.
how can I set write buffer?
Do you want to
[1] write bytes in exactly the order you gave above (portable
result), or
[2] write an unsigned long value to the file (non-portable
result)?
---------------------------------------------------
typedef unsigned char UCHAR;
int iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRO NLY,S_IREAD|S_IWRITE);
UCHAR buffer[5]; //???????????
write(iFD,buffer,5);
---------------------------------------------------


Neither open nor write are standard C functions.
What you want is something like:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
unsigned char buf[] = { 0x00, 0x04, 0x07, 0x00 };
unsigned long ul = 0x00040700UL;
FILE *fp;

if ( ( fp = fopen( "foo", "wb" ) ) != NULL )
{
fwrite( buf, sizeof buf, 1, fp ); /* [1] */
fwrite( &ul, sizeof ul, 1, fp ); /* [2] */
fclose( fp );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}

HTH
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3
"cylin" <cy***@avant.com.tw> writes:
Dear all,

I open a binary file and want to write 0x00040700 to this file.
how can I set write buffer?
Use shifts and mask operations (bitwise AND) to extract the individual
bytes. The details depend on the byte order in which you want to write
to the file.
---------------------------------------------------
typedef unsigned char UCHAR;
int iFD=open(szFileName,O_CREAT|O_BINARY|O_TRUNC|O_WRO NLY,S_IREAD|S_IWRITE);
No such function in standard C. Use `fopen'.
UCHAR buffer[5]; //???????????
write(iFD,buffer,5);
No such function in standard C. Use `fwrite'.
---------------------------------------------------


This program should give you same hints:
#include <stdlib.h>
#include <stdio.h>

int main (void)
{
const unsigned long value = 0x00040700;
unsigned char buffer [4];
FILE *f;

/* This as known as "big-endian" byte order. */
buffer [0] = value >> 24;
buffer [1] = (value >> 16) & 0xFF;
buffer [2] = (value >> 8) & 0xFF;
buffer [3] = value & 0xFF;

f = fopen ("testfile", "wb");
if (f != NULL)
{
if (fwrite (buffer, sizeof *buffer, sizeof buffer, f) < sizeof buffer
|| fclose (f) == EOF)
{
fputs ("Error writing to testfile.\n", stderr);
return EXIT_FAILURE;
}
}
else
{
fputs ("Cannot open testfile for writing.\n", stderr);
return EXIT_FAILURE;
}

return 0;
}
Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #4
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
if (fwrite(buf, sizeof *buf, sizeof buf/sizeof *buf, outfile) !=
sizeof buf/sizeof *buf) {
puts("Handle a write error here.");
}
fclose(outfile);
/* For critical applications, you should even check the return value
of fclose(), but I rarely do this. */


Why do you check the return value of `fwrite' then? Most operating
systems don't write immediately to the underlying device when `fwrite'
is called, but have some buffering mechanism. Therefore, an error is
far more likely to show up in `fclose' than in `fwrite' on such systems.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
Martin Dickopp <ex****************@zero-based.org> writes:
if (fwrite (buffer, sizeof *buffer, sizeof buffer, f) < sizeof buffer


That's inconsistent. Make that:

if (fwrite (buffer, 1, sizeof buffer, f) < sizeof buffer

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #6
Great. Thank all.

I use low-level I/O functions because I want to the speed faster than
stardard I/O functions.
Can't low-level I/O functions do this case?

Regards,
cylin.
Nov 14 '05 #7
"cylin" <cy***@avant.com.tw> writes:
I use low-level I/O functions because I want to the speed faster than
stardard I/O functions.
Can't low-level I/O functions do this case?


They can, but they're off-topic in comp.lang.c, which is only about
standard C.

<OT>
They are also harder to use correctly. Note, e.g., that it is not
necessarily an indication of error if the POSIX function `write' writes
less bytes than requested.
</OT>

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #8
cylin <cy***@avant.com.tw> scribbled the following:
Great. Thank all. I use low-level I/O functions because I want to the speed faster than
stardard I/O functions.
Can't low-level I/O functions do this case?


First of all, your "low-level I/O functions" might not even be available
on all platforms. Second of all, there is no guarantee they will be any
faster than standard I/O functions. They could even be slower.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"C++ looks like line noise."
- Fred L. Baube III
Nov 14 '05 #9
Martin Dickopp wrote:
"cylin" <cy***@avant.com.tw> writes:
I open a binary file and want to write 0x00040700 to this file.
how can I set write buffer?
Use shifts and mask operations (bitwise AND) to extract the
individual bytes. The details depend on the byte order in which
you want to write to the file.

.... snip ...
This program should give you same hints:

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
const unsigned long value = 0x00040700;
unsigned char buffer [4];
FILE *f;

/* This as known as "big-endian" byte order. */
buffer [0] = value >> 24;
buffer [1] = (value >> 16) & 0xFF;
buffer [2] = (value >> 8) & 0xFF;
buffer [3] = value & 0xFF;

f = fopen ("testfile", "wb");
if (f != NULL)
{
if (fwrite (buffer, sizeof *buffer, sizeof buffer, f) < sizeof buffer
|| fclose (f) == EOF)
{
fputs ("Error writing to testfile.\n", stderr);
return EXIT_FAILURE;
}
}
else
{
fputs ("Cannot open testfile for writing.\n", stderr);
return EXIT_FAILURE;
}

return 0;
}


There is nothing wrong with your code above, and it illustrates
most things admirably, I suggest that the use of embedded tests
will facilitate a clearer order of things. This is a style
question, not a flame, and just a suggestion. My version follows:

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
const unsigned long value = 0x00040700;
unsigned char buffer [4];
FILE *f;

/* This as known as "big-endian" byte order. */
buffer [0] = value >> 24;
buffer [1] = (value >> 16) & 0xFF;
buffer [2] = (value >> 8) & 0xFF;
buffer [3] = value & 0xFF;

if (!(f = fopen("testfile", "wb"))) {
fputs("Cannot open testfile for writing.\n", stderr);
}
else if ((fwrite(buffer, sizeof *buffer, sizeof buffer, f)
< sizeof buffer)
|| (EOF == fclose(f))) {
fputs("Error writing to testfile.\n", stderr);
}
else {
return 0;
}
return EXIT_FAILURE;
}

although the neophyte is still going to have trouble comprehending
the combined fwrite/fclose failure test. I have also added the
odd extraneous parentheses to make the meanings explicit.

This now follows the pattern "if phasefails exit else nextphase".

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
Nov 14 '05 #10
In <40*****************@news.individual.net> rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
fclose(outfile);
/* For critical applications, you should even check the return value
of fclose(), but I rarely do this. */


Bad idea: most of the things that can go wrong (after fopen succeeded)
happen at fclose time.

To optimise the error checking, when I generate an output file in a
compact succession of operations, I only check fclose and a fflush call
immediately preceding it (which is probably redundant).

OTOH, long running programs that generate output constantly (e.g. one
printf call per main loop iteration) should check each output call,
even if it's on stdout (that gets redirected to a file when the program
is run in non-interactive mode). No point in continuing the execution
if the program can no longer generate output.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11
In <c6************@ID-230325.news.uni-berlin.de> "cylin" <cy***@avant.com.tw> writes:
I use low-level I/O functions because I want to the speed faster than
stardard I/O functions.
Where did you get this silly idea from? The low-level I/O functions have
a relatively high cost and the stardard I/O functions are designed to
minimise the number of low-level I/O function calls, by doing some local
buffering.

As a result of this, it is trivially easy for the ignorant to slow down
his I/O by one order of magnitude, while an expert can only speed up his
I/O by a small percentage, by using the low-level functions instead of the
standard ones and removing one level of buffering, when and where it is
not necessary.
Can't low-level I/O functions do this case?


Why bother?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12
kal
> Use shifts and mask operations (bitwise AND) to extract the individual
bytes. The details depend on the byte order in which you want to write
to the file.


IMHO one is better off using "htnl()." This will work fine so long as
all you are writing are 32 bit values.
Nov 14 '05 #13
kal <k_*****@yahoo.com> spoke thus:
IMHO one is better off using "htnl()." This will work fine so long as
all you are writing are 32 bit values.


I suspect you meant htonl().

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14
k_*****@yahoo.com (kal) writes:
Use shifts and mask operations (bitwise AND) to extract the individual
bytes. The details depend on the byte order in which you want to write
to the file.


IMHO one is better off using "htnl()."


Wheather such a non-standard method of doing things is preferable when a
standard method exists is certainly debatable.

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #15
Martin Dickopp <ex****************@zero-based.org> wrote:
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
if (fwrite(buf, sizeof *buf, sizeof buf/sizeof *buf, outfile) !=
sizeof buf/sizeof *buf) {
puts("Handle a write error here.");
}
fclose(outfile);
/* For critical applications, you should even check the return value
of fclose(), but I rarely do this. */


Why do you check the return value of `fwrite' then? Most operating
systems don't write immediately to the underlying device when `fwrite'
is called, but have some buffering mechanism. Therefore, an error is
far more likely to show up in `fclose' than in `fwrite' on such systems.


Good question. Guess I'm just too used to interactive output.

Richard
Nov 14 '05 #16
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
kal <k_*****@yahoo.com> spoke thus:
IMHO one is better off using "htnl()." This will work fine so long as
all you are writing are 32 bit values.


I suspect you meant htonl().


And _I_ suspect that function is not ISO.

Richard
Nov 14 '05 #17
Richard Bos <rl*@hoekstra-uitgeverij.nl> spoke thus:
And _I_ suspect that function is not ISO.


Going off-topic doesn't have to mean making things up :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #18
kal
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote
IMHO one is better off using "htnl()."


I suspect you meant htonl().


Yes, it is "htonl()." Thank you very much. It was my mistake.

Are there any equivalent functions in the standard C library?

Is there even the concept of byte ordering in C?
Nov 14 '05 #19
In <a5**************************@posting.google.com > k_*****@yahoo.com (kal) writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote
> IMHO one is better off using "htnl()."
I suspect you meant htonl().


Yes, it is "htonl()." Thank you very much. It was my mistake.

Are there any equivalent functions in the standard C library?


Nope.
Is there even the concept of byte ordering in C?


Nope. In theory the bits could be scattered all over the place, rather
than being nicely grouped in bytes, i.e. the physical representation of
a short could be:

b0 b7 b5 sign b4 b3 b1 b10 | b2 b14 b8 b9 b6 b12 b13 b12
---------------------------+----------------------------
first byte second byte

as long as all the operations generate the specified results (and unsigned
short replaces the sign bit by bit 15). Of course, this is not going
to happen in practice, but the standard is perfectly happy with it.
Some very popular (at the time) old architectures had glitches at the
byte ordering level (neither big endian nor little endian for 32-bit
integers) but they are gone now.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #20
k_*****@yahoo.com (kal) writes:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote
IMHO one is better off using "htnl()." I suspect you meant htonl().

Yes, it is "htonl()." Thank you very much. It was my mistake.

Are there any equivalent functions in the standard C library?


Not really.
Is there even the concept of byte ordering in C?


Well, sort of. Any C object can be viewed as an array of bytes
(unsigned char), but the standard doesn't say much about what those
bytes are going to look like. For example:

#include <stdio.h>

int main(void)
{
unsigned int x = 0x12345678;
unsigned char *ptr = (unsigned char*)&x;
int i;

printf("x = 0x%x\n", x);
for (i = 0; i < sizeof x; i ++) {
printf(" 0x%x", (unsigned int)ptr[i]);
}
printf("\n");
return 0;
}

On one platform I just tried, the output is:

x = 0x12345678
0x12 0x34 0x56 0x78

On another, it's:

x = 0x12345678
0x78 0x56 0x34 0x12

but a valid implementation could produce:

x = 0x12345678
0x23 0x12 0x56 0x78

or any of a number of other possibilities.

But don't be afraid to use system-specific functions if they're
appropriate. If you need to use something like htonl(), chances are
your program is already doing plenty of stuff that's not directly
supported by the standard C library (htonl() is mostly used in
networking, and all networking support is system-specific). (Just
don't expect detailed advice about it in comp.lang.c.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #21

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

Similar topics

3
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...
4
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...
1
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 <<...
5
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...
3
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...
6
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...
2
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...
13
by: zach | last post by:
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...
6
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 =...
1
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...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...

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.