我有一些模拟到数字转换器数据,它自动写在我想要读出的二进制文件中。在设备附带的使用说明书中,有一个示例C代码可以做到这一点,但我尝试使用它并且它不会编译。由于这是我第一次做这样的事情或第一次使用C,我需要一些帮助。
手册说:.CHN光谱数据文件包含32位整数的MCB的逐个通道内容。标头长度为32个字节。文件的下一部分包含存储为32位整数的频谱。文件中没有记录分隔符。频谱记录的数量由频谱中的信道数决定。 给出的代码是:
/*Sample program compatible with Microsoft and Borland C*/
/* to read header and channel data from a .CHN data file. */
#include <stdio.h>
#define .CHN -1
main(argc, argv)
int argc;
char *argv[];
{
char acq_time[32]; /* buffer for time, date*/
int f_type; /*.CHN file type*/
unsigned int chan_offset,/* beginning channel number*/
count, /*loop counter*/
mca_num, /*1-4 are valid*/
num_chans, /* no. of data channels*/
num_writ, /* no. of bytes written out*/
segment; /* segment number*/
long int livetime, /*20 ms tics of livetime*/
realtime, /*20 ms tics of realtime*/
chan_data; /*stores channel data*/
FILE *f_pointer;
if(argc!=2){
printf("USAGE: readchn filename.chn\n");
exit(1);
}
f_pointer = fopen(argv[1], "rb");
/* Header Data*/
/* output header info from .CHN file*/
/* read filetype -1 (.CHN) */
fread(&f_type, sizeof(int), 1, f_pointer);
if(f_type != .CHN) {
printf("Not a valid file \n");
return -1;
}
fread(&mca_num, sizeof(int), 1, f_pointer); /*MCA number*/
fread(&segment, sizeof(int), 1, f_pointer); /* segment #*/
fread(acq_time+12, sizeof(char), 1, f_pointer);/*start time*/
fread(&realtime, sizeof(long), 1, f_pointer);/*20ms ticks*/
fread(&livetime, sizeof(long), 1, f_pointer);/*20ms ticks*/
fread(acq_time, sizeof(char), 2, f_pointer);/*start date*/
fread(acq_time+2, sizeof(char), 3, f_pointer);/*start month*/
fread(acq_time+5, sizeof(char), 3, f_pointer);/*start year*/
fread(acq_time+8, sizeof(char), 2, f_pointer);/*start hour*/
fread(acq_time+10, sizeof(char), 2, f_pointer);/*start mins*/
fread(&chan_offset, sizeof(int), 1, f_pointer); /*offset*/
fread(&num_chans, sizeof(int), 1, f_pointer); /*#channels*/
printf("TYPE = %4i MCA# %2i\n", f_type, mca_num);
printf("SEGMENT # %3i\n", segment);
printf("REALTIME = %10li seconds\n", realtime/50);
printf("LIVETIME = %10li seconds\n", livetime/50);
printf("DATA COLLECTED AT ");
fwrite(acq_time+8, sizeof(char), 2, stdout);
putchar(':');
fwrite(acq_time+10, sizeof(char), 2, stdout);
putchar(':');
fwrite(acq_time+12, sizeof(char), 2, stdout);
printf("ON");
fwrite(acq_time, sizeof(char), 2, stdout);
putchar('-');
fwrite(acq_time+2, sizeof(char), 3, stdout);
putchar('-');
fwrite(acq_time+5, sizeof(char), 2, stdout);
printf("\nSTARTING CHANNEL = %6in", chan_offset);
printf("\nNUMBER OF CHANNELS = %6i\n\n", num_chans);
/* Channel Data*/
/* Output channel data from .CHN file*/
printf("CHANNEL DATA\n");
for(count = 0; count < num_chans; count++){
if((count % 6) ==0)
printf("n%7i", count);
fread(&chan_data, sizeof(long), 1, f_pointer);
printf("%11i", chan_data);
}
fcloseall();
}
给定的代码不会在XCode中编译。
鉴于文件结构,有没有办法从命令行查看文件(1和0)?我尝试使用
xxd -b <filename>
但输出与文件描述看起来不一致,没有明显的标题。以下是输出的前几行:
0000000: 11111111 11111111 00000001 00000000 00000001 00000000 ......
0000006: 00110001 00110001 10111101 11011101 00110011 00000000 11..3.
000000c: 01100111 01000100 00101100 00000000 00110010 00110011 gD,.23
0000012: 01000110 01100101 01100010 00110001 00110111 00110001 Feb171
0000018: 00110001 00110110 00110101 00110000 00000000 00000000 1650..
000001e: 00000000 00001000 00000000 00000000 00000000 00000000 ......
0000024: 00000000 00000000 00000000 00000000 00000000 00000000 ......
000002a: 00000000 00000000 00000000 00000000 00000000 00000000 ......
0000030: 00000000 00000000 00000000 00000000 00000000 00000000 ......
非常感谢任何帮助!