473,698 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(szFile Name,O_CREAT|O_ BINARY|O_TRUNC| O_WRONLY,S_IREA D|S_IWRITE);
UCHAR buffer[5]; //???????????
write(iFD,buffe r,5);
---------------------------------------------------

Thanks.

Regards,
cylin.
Nov 14 '05
20 5582
In <40************ *****@news.indi vidual.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.co m.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14
k_*****@yahoo.c om (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.c yberspace.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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #18
kal
Christopher Benson-Manica <at***@nospam.c yberspace.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.c om (kal) writes:
Christopher Benson-Manica <at***@nospam.c yberspace.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

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

Similar topics

3
15665
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
3485
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
1871
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
3655
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
8529
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
7367
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...
13
18191
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 string of text";
6
5236
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
3902
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
8598
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
9152
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
8856
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7709
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
6515
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
5858
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
4613
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3037
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
2
2321
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.