能否实现一个函数:给出任意结构体参数,打印出其内容

影子传说_Hades 2010-05-15 11:05:42
实现一个函数

给出任意到结构体,都可以打印出结构体元素名称和改元素对应的值

在java里面可以用反射实现,如果在C语言里面怎么实现这个功能呢?
...全文
255 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2010-05-15
  • 打赏
  • 举报
回复
C语言不提供这种功能,只能自己实现。
arong1234 2010-05-15
  • 打赏
  • 举报
回复
C中没有什么巧妙的方法,你只能一个变量一个变量输出,没有诸如反射之类的可以直接使用的方法
影子传说_Hades 2010-05-15
  • 打赏
  • 举报
回复
我期望的输出文本类似与

PSDimage.channels = 3
PSDimage.transparency(False is 0) = 0
PSDimage.rows(pix) = 102
PSDimage.columns(pix) = 999
PSDimage.bps = 8
PSDimage.color_mode = 3
PSDimage.base_type = 0
PSDimage.comp_mode = 63064
PSDimage.color_map(4ubyte) = 0xa6becd
PSDimage.color_map_len = 0
PSDimage.color_map_entries = 0
PSDimage.image_res_start = 34
PSDimage.image_res_len = 22198
PSDimage.mask_layer_start = 22236
PSDimage.mask_layer_len = 117036
PSDimage.num_layers = 3
PSDimage.layer_data_start = 23426
PSDimage.layer_data_len = 115789
PSDimage.merged_image_start = 139272
PSDimage.merged_image_len = 10428
PSDimage.no_icc = 0
PSDimage.layer_state = 2
PSDimage.alpha_display_count = 0
PSDimage.alpha_id = 0
PSDimage.alpha_id_count = 0
PSDimage.quick_mask_id = 0
影子传说_Hades 2010-05-15
  • 打赏
  • 举报
回复
下面是结构体的定义


/* PSD File data structures */
typedef struct
{
guint16 channels; /* Number of channels: 1- 56 */
gboolean transparency; /* Image has merged transparency alpha channel */
guint32 rows; /* Number of rows: 1 - 30000 */
guint32 columns; /* Number of columns: 1 - 30000 */
guint16 bps; /* Bits per channel: 1, 8 or 16 */
guint16 color_mode; /* Image colour mode: {PSDColorMode} */
GimpImageBaseType base_type; /* Image base colour mode: (GIMP) */
guint16 comp_mode; /* Merged image compression mode */
guchar *color_map; /* Colour map data */
guint32 color_map_len; /* Colour map data length */
guint32 color_map_entries; /* Colour map number of entries */
guint32 image_res_start; /* Image resource block start address */
guint32 image_res_len; /* Image resource block length */
guint32 mask_layer_start; /* Mask & layer block start address */
guint32 mask_layer_len; /* Mask & layer block length */
gint16 num_layers; /* Number of layers */
guint32 layer_data_start; /* Layer pixel data start */
guint32 layer_data_len; /* Layer pixel data length */
guint32 merged_image_start; /* Merged image pixel data block start address */
guint32 merged_image_len; /* Merged image pixel data block length */
gboolean no_icc; /* Do not use ICC profile */
guint16 layer_state; /* Active layer number counting from bottom up */
GPtrArray *alpha_names; /* Alpha channel names */
PSDchanneldata **alpha_display_info; /* Alpha channel display info */
guint16 alpha_display_count; /* Number of alpha channel display info recs */
guint32 *alpha_id; /* Alpha channel ids (tattoos) */
guint16 alpha_id_count; /* Number of alpha channel id items */
guint16 quick_mask_id; /* Channel number containing quick mask */
} PSDimage;
影子传说_Hades 2010-05-15
  • 打赏
  • 举报
回复
背景:
利用GIMP来解析PSD文件,然后把PSD元信息输出到文本中,我目前是这么做的

结构体是GIMP包定义的,我需要输出其结构体的内容到文本中

下面是我目前的办法,难道没有好的办法解决?

#include "config.h"

#include <string.h>
#include <errno.h>

#include <glib/gstdio.h>
#include <libgimp/gimp.h>

#include "psd.h"
#include "psd-util.h"
#include "psd-image-res-load.h"
#include "psd-layer-res-load.h"
#include "psd-load.h"

#include "libgimp/stdplugins-intl.h"

void print_all_psd(FILE *f, struct stat st, PSDimage img_a, PSDlayer **lyr_a,
gint32 image_id, GError *error);
static int save_psdimage_to_file(const char* fileName, char *linefeedp,
PSDimage img_a);

void print_all_psd(FILE *f, struct stat st, PSDimage img_a, PSDlayer **lyr_a,
gint32 image_id, GError *error) {
save_psdimage_to_file("/home/hades/PSDimageValue.properties", "\r\n", img_a);
}

int save_psdimage_to_file(const char* fileName, char *linefeedp, PSDimage img_a) {
FILE *stream;
stream = fopen(fileName, "w");
if (stream == NULL) {
fprintf(stderr, "Cannot open output file.\r\n");
return 1;
}

//guint16 channels; /* Number of channels: 1- 56 */
fprintf(stream, "PSDimage.channels = %d %s", img_a.channels, linefeedp);
printf("PSDimage.channels = %d %s", img_a.channels, linefeedp);
// gboolean transparency; /* Image has merged transparency alpha channel */
fprintf(stream, "PSDimage.transparency(False is 0) = %d %s",
img_a.transparency, linefeedp);
printf("PSDimage.transparency(False is 0) = %d %s", img_a.transparency,
linefeedp);
// guint32 rows; /* Number of rows: 1 - 30000 */
fprintf(stream, "PSDimage.rows(pix) = %d %s", img_a.rows, linefeedp);
printf("PSDimage.rows(pix) = %d %s", img_a.rows, linefeedp);
// guint32 columns; /* Number of columns: 1 - 30000 */
fprintf(stream, "PSDimage.columns(pix) = %d %s", img_a.columns, linefeedp);
printf("PSDimage.columns(pix) = %d %s", img_a.columns, linefeedp);
// guint16 bps; /* Bits per channel: 1, 8 or 16 */
fprintf(stream, "PSDimage.bps = %d %s", img_a.bps, linefeedp);
printf("PSDimage.bps = %d %s", img_a.bps, linefeedp);
// guint16 color_mode; /* Image colour mode: {PSDColorMode} */
fprintf(stream, "PSDimage.color_mode = %d %s", img_a.color_mode, linefeedp);
printf("PSDimage.color_mode = %d %s", img_a.color_mode, linefeedp);
// GimpImageBaseType base_type; /* Image base colour mode: (GIMP) */
fprintf(stream, "PSDimage.base_type = %d %s", img_a.base_type, linefeedp);
printf("PSDimage.base_type = %d %s", img_a.base_type, linefeedp);
// guint16 comp_mode; /* Merged image compression mode */
fprintf(stream, "PSDimage.comp_mode = %d %s", img_a.comp_mode, linefeedp);
printf("PSDimage.comp_mode = %d %s", img_a.comp_mode, linefeedp);
// guchar *color_map; /* Colour map data */
fprintf(stream, "PSDimage.color_map(4ubyte) = 0x%x %s", img_a.color_map,
linefeedp);
printf("PSDimage.color_map(4ubyte) = 0x%x %s", img_a.color_map, linefeedp);
// guint32 color_map_len; /* Colour map data length */
fprintf(stream, "PSDimage.color_map_len = %d %s", img_a.color_map_len,
linefeedp);
printf("PSDimage.color_map_len = %d %s", img_a.color_map_len, linefeedp);
// guint32 color_map_entries; /* Colour map number of entries */
fprintf(stream, "PSDimage.color_map_entries = %d %s",
img_a.color_map_entries, linefeedp);
printf("PSDimage.color_map_entries = %d %s", img_a.color_map_entries,
linefeedp);
// guint32 image_res_start; /* Image resource block start address */
fprintf(stream, "PSDimage.image_res_start = %d %s", img_a.image_res_start,
linefeedp);
printf("PSDimage.image_res_start = %d %s", img_a.image_res_start, linefeedp);
// guint32 image_res_len; /* Image resource block length */
fprintf(stream, "PSDimage.image_res_len = %d %s", img_a.image_res_len,
linefeedp);
printf("PSDimage.image_res_len = %d %s", img_a.image_res_len, linefeedp);
// guint32 mask_layer_start; /* Mask & layer block start address */
fprintf(stream, "PSDimage.mask_layer_start = %d %s",
img_a.mask_layer_start, linefeedp);
printf("PSDimage.mask_layer_start = %d %s", img_a.mask_layer_start,
linefeedp);

// guint32 mask_layer_len; /* Mask & layer block length */
fprintf(stream, "PSDimage.mask_layer_len = %d %s", img_a.mask_layer_len,
linefeedp);
printf("PSDimage.mask_layer_len = %d %s", img_a.mask_layer_len, linefeedp);
// gint16 num_layers; /* Number of layers */
fprintf(stream, "PSDimage.num_layers = %d %s", img_a.num_layers, linefeedp);
printf("PSDimage.num_layers = %d %s", img_a.num_layers, linefeedp);
// guint32 layer_data_start; /* Layer pixel data start */
fprintf(stream, "PSDimage.layer_data_start = %d %s",
img_a.layer_data_start, linefeedp);
printf("PSDimage.layer_data_start = %d %s", img_a.layer_data_start,
linefeedp);
// guint32 layer_data_len; /* Layer pixel data length */
fprintf(stream, "PSDimage.layer_data_len = %d %s", img_a.layer_data_len,
linefeedp);
printf("PSDimage.layer_data_len = %d %s", img_a.layer_data_len, linefeedp);
// guint32 merged_image_start; /* Merged image pixel data block start address */
fprintf(stream, "PSDimage.merged_image_start = %d %s",
img_a.merged_image_start, linefeedp);
printf("PSDimage.merged_image_start = %d %s", img_a.merged_image_start,
linefeedp);
// guint32 merged_image_len; /* Merged image pixel data block length */
fprintf(stream, "PSDimage.merged_image_len = %d %s",
img_a.merged_image_len, linefeedp);
printf("PSDimage.merged_image_len = %d %s", img_a.merged_image_len,
linefeedp);
// gboolean no_icc; /* Do not use ICC profile */
fprintf(stream, "PSDimage.no_icc = %d %s", img_a.no_icc, linefeedp);
printf("PSDimage.no_icc = %d %s", img_a.no_icc, linefeedp);
// guint16 layer_state; /* Active layer number counting from bottom up */
fprintf(stream, "PSDimage.layer_state = %d %s", img_a.layer_state,
linefeedp);
printf("PSDimage.layer_state = %d %s", img_a.layer_state, linefeedp);
// guint16 alpha_display_count; /* Number of alpha channel display info recs */
fprintf(stream, "PSDimage.alpha_display_count = %d %s",
img_a.alpha_display_count, linefeedp);
printf("PSDimage.alpha_display_count = %d %s", img_a.alpha_display_count,
linefeedp);
// guint32 *alpha_id; /* Alpha channel ids (tattoos) */
fprintf(stream, "PSDimage.alpha_id = %d %s", img_a.alpha_id, linefeedp);
printf("PSDimage.alpha_id = %d %s", img_a.alpha_id, linefeedp);
// guint16 alpha_id_count; /* Number of alpha channel id items */
fprintf(stream, "PSDimage.alpha_id_count = %d %s", img_a.alpha_id_count,
linefeedp);
printf("PSDimage.alpha_id_count = %d %s", img_a.alpha_id_count, linefeedp);
// guint16 quick_mask_id; /* Channel number containing quick mask */
fprintf(stream, "PSDimage.quick_mask_id = %d %s", img_a.quick_mask_id,
linefeedp);
printf("PSDimage.quick_mask_id = %d %s", img_a.quick_mask_id, linefeedp);
// GPtrArray *alpha_names; /* Alpha channel names */

// PSDchanneldata **alpha_display_info; /* Alpha channel display info */

return 0;
}

2010-05-15
  • 打赏
  • 举报
回复
如果你只是想要调试程序的话,直接用调试器就好了
如果你真的要在程序里面使用反射,还是回去用 Java 吧
C 虽然可以做这种事情,不过通常还是不要用 C 做这种事情……
影子传说_Hades 2010-05-15
  • 打赏
  • 举报
回复
我需要输出结构体的名称和其对应到值,如果不能用简单循环实现,那结构体有100个元素我就要打印100次,太痛苦了吧
2010-05-15
  • 打赏
  • 举报
回复
那就自己实现反射吧……
你不会真的想这么做的~
影子传说_Hades 2010-05-15
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 mymtom 的回复:]
C语言不提供这种功能,只能自己实现。
[/Quote]

是不是我只能一个元素一个元素打印了?

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧