结构解析器 [英] a struct parser
问题描述
是否有一个工具,在给定结构定义的情况下,生成一个函数
,它解析此结构的二进制数据和一个命令可以用
根据这个结构的
字段的用户指定值构造二进制数据吗?
谢谢。
Wei
Hi,
Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?
Thank you.
Wei
推荐答案
Weiguang Shi写道:
Weiguang Shi wrote:
是否有一个工具,给定一个结构定义,生成一个解析此结构的二进制数据的函数和一个可用于构造二进制的命令数据是根据用户指定的值来计算这个结构的
字段吗?
Hi,
Is there a tool that, given a struct definition, generates a function
that parses binary data of this struct and a command that can be used
to construct binary data according to user-specified values for the
fields of this struct?
我认为它叫做编译器。我真的不是故意讽刺地说,
但是我不确定你是在彻底解释你的问题。对于
阅读,听起来像你想要的东西:
#include< stdio.h>
#define NUM_FOO 50
struct foo {
int a;
char b;
unsigned int c;
};
int
main(无效)
{
struct foo f [NUM_FOO];
while(fread(f,sizeof * f,NUM_FOO,stdin)== NUM_FOO){
int i ;
for(i = 0; i< NUM_FOO; i ++)
printf(" a =%d,b =%c,c =%u \ n" ;,
f [i] .a,f [i] .b,f [i] .c);
}
返回错误(stdin);
}
如果您的输入流已创建,则需要小心
由不同的编译器,它可能以不同的方式对齐
结构的字段。
~
I think it''s called a compiler. I really don''t mean that
sarcastically,
but I''m not sure you''re explaining your question thoroughly. For
reading, it sounds like you want something like:
#include <stdio.h>
#define NUM_FOO 50
struct foo {
int a;
char b;
unsigned int c;
};
int
main(void)
{
struct foo f[NUM_FOO];
while ( fread (f, sizeof *f, NUM_FOO, stdin) == NUM_FOO) {
int i;
for (i=0; i< NUM_FOO; i++)
printf("a = %d, b = %c, c = %u\n",
f[i].a, f[i].b, f[i].c);
}
return ferror(stdin);
}
You''ll need to be careful if your input stream was created
by a different compiler, which may have aligned the fields of the
structure differently.
~
谢谢反馈意见以及让事情透视的程序。
我想要的只是手动执行/>
Thanks for the feedback and a program to put things in perspective.
What I want is to do manually only
struct foo {
int a;
char b;
unsigned int c;
};
struct foo {
int a;
char b;
unsigned int c;
};
我想要一个命令来创造''''二进制foo'也是这样的。
我只是绝望地懒惰;-)
Wei
And I want a command to ``create'''' binary foo''s out of this, too.
I''m just hopelessly lazy ;-)
Wei
Weiguang Shi写道:
Weiguang Shi wrote:
感谢您的反馈和程序把事情放在透视中。
我想要的只是手动完成
Thanks for the feedback and a program to put things in perspective.
What I want is to do manually only
struct foo {
int a;
char b ;
unsigned int c;
};
struct foo {
int a;
char b;
unsigned int c;
};
我想要一个命令``'''''''''''''''''''''''''''''''''' 。
我只是绝望地懒惰; - )
And I want a command to ``create'''' binary foo''s out of this, too.
I''m just hopelessly lazy ;-)
你在跟谁说话?回复时引用一些上下文。
Who are you talking to? Quote some context when replying.
这篇关于结构解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!