写一个二进制文件? [英] write a binary file?

查看:75
本文介绍了写一个二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


我打开一个二进制文件,想把0x00040700写入这个文件。

如何设置写缓冲区?

--------------------------------------------- ------

typedef unsigned char UCHAR;

int iFD = open(szFileName,O_CREAT | O_BINARY | O_TRUNC | O_WRO NLY,S_IREAD | S_IWRITE);

UCHAR buffer [5]; // ??????????

写(iFD,缓冲区,5);

------------ ---------------------------------------


谢谢。


问候,

cylin。

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.

推荐答案

" cylin" < CY *** @ avant.com.tw>写道:
"cylin" <cy***@avant.com.tw> wrote:
我打开一个二进制文件,想写0x00040700到这个文件。
如何设置写缓冲区?


在ISO C中不是这样的。你所拥有的是系统特定的,或者是
POSIX或者不是 - 相当于POSIX-M
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);


匈牙利表示法。双倍的。 HN过去常常声明每个人读取你的代码,包括编译器,已经知道:

你的标识符类型。三倍,然后去自信店和

买一些。

UCHAR缓冲[5]; // ???????????


混合声明和可执行语句在C99和C ++中是合法的,

但不在C89中。小心嗤之以鼻。

写(iFD,缓冲区,5);
.
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);




不知道如何使用低级别来解决这个问题,不太可能到港口

功能。在_real_ C中,你会做这样的事情:


#include< stdio.h>


unsigned char buf [5 ];

FILE * outfile;


if(!(outfile = fopen(filename," wb")){

puts(这是你处理文件打开错误的地方。);

} else {

if(fwrite(buf,sizeof) * buf,sizeof buf / sizeof * buf,outfile)!=

sizeof buf / sizeof * buf){

puts(在此处理写入错误。 );

}

/ *或者,因为你知道buf是一个unsigned char数组而且

因此sizeof * buf必须是1:

fwrite(缓冲区,1,sizeof buf,outfile);

* /

fclose(outfile);

/ *对于关键应用程序,你甚至应该检查fclose()的返回值

,但我很少这样做。* /

}


Issa dat si''ple。


Richard



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


" cylin"< cy ** * @ AV ant.com.tw>写道:
"cylin" <cy***@avant.com.tw> wrote:

我打开一个二进制文件,想把0x00040700写入这个文件。
如何设置写缓冲区?


你想要

[1]按照你上面给出的顺序写入字节(便携式

结果),或者

[2]写一个unsigned long值给文件(非便携式

结果)?

-------- -------------------------------------------
typedef unsigned char UCHAR;
int iFD = open(szFileName,O_CREAT | O_BINARY | O_TRUNC | O_WRO NLY,S_IREAD | S_IWRITE);
UCHAR buffer [5]; // ??????????
写(iFD,缓冲区,5);
-------------------- -------------------------------

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);
---------------------------------------------------




既不开放也不开放写是标准的C函数。

你想要的是:


#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);

返回EXIT_SUCCESS;

}

返回EXIT_FAILURE ;

}


HTH

问候

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http://www.faqs.org/faqs/C-faq/faq/

clc OT指南: http ://benpfaff.org/writings/clc/off-topic.html



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


这篇关于写一个二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆