解析数据帧 c语言编写

#include “stdio.h”
#include
FILE *fp;
FILE *fp1; // 头文件#include
int main(void)
{
short a[10000];
short element;
int i = 0;
int n;
fp = fopen(“input_file.txt”, “rb”);//打开文件
while (!feof(fp))//while循环读取里面的数据
{

    element = getc(fp);
    a[i++] = element;
    printf("%2x", element);
}
n = i;

printf("\n前导码:");
i = 0;
while (a[i++])//解析前导码
{
    if (i < 7)
        printf("%2x ", a[i]);
}

printf("\n帧定界符:");
i = 0;
while (a[i++])// 帧定界符
{
    if (i >= 7 && i < 8)
        printf("%2x", a[i]);
}
printf("\n目的地址:");
i = 0;
while (a[i++])// 目的地址
{
    if (i <= 13 && i >= 8)
        printf("%2x-", a[i]);
}
printf("\b ");
printf("\n源地址:");
i = 0;
while (a[i++])// 源地址
{
    if (i <= 19 && i >= 14)
        printf("%2x-", a[i]);
}
printf("\b ");

printf("\n长度字节:");
i = 0;
while (a[i++])
{
    if (i <= 21 && i >= 20)
        printf("%2x  ", a[i]);
}

printf("\n数据字段:");
i = 22;
while (i < n - 5 && i >= 22)//数据字段 
{

    printf("%c", a[i++]);
}
printf("\n帧校验字段:");
i = n - 1;
while (i < n)// 帧校验字段
{
    printf("%2x  ", a[i++]);
}
FILE *fpWrite = fopen("output.txt", "w");
if (fpWrite == NULL)
{
    return 0;
}
i = 22;
while (i < n - 5 && i >= 22)
{
    fprintf(fpWrite, "%c", a[i++]);
}
fclose(fpWrite);//关闭读取文件 
fclose(fp);
return 0;

}

在这里插入代码片

你可能感兴趣的:(解析数据帧 c语言编写)