以@打头的表示地址,*号表示的是数据:
如下一段(原文件的片段)将其转换成另外一种格式存到另外一个文件里面
要求:①遇到‘@’打印其第5~15位,遇到偶数‘@’不打印;
②遇到‘*’打印第16~17位。
③在Turbo C下能运行。
@0640 0010000000 AA
*08000000000000A50053
*08000000000000960062
*080000000000005A009E
*0800000000000069008F
*080000000000005A009E
*080000000000005A009E
*080000000000009E005A
*080000000000002000D8
@06400010000008A2
*080000000000000000F8
*080000000000001000E8
*080000000000000000F8
*080000000000000000F8
*080000000000000000F8
*080000000000000000F8
*080000000000000800F0
*080000000000000000F8
转换后格式(即要得到的格式)
0010000000 A5 96 5A 69 5A 5A 9E 20 00 10 00 00 00 00 08 00
#include
#include
#include
#define BUF_LEN 50
int main()
{
int line_num = 1;
FILE *fp1, fp2;
char buf[BUF_LEN];
char tmp_buf[BUF_LEN];
char *p;
if ((fp1 = fopen("file1.txt", "r")) == NULL) {
printf("src file open err\n");
}
while((fgets(buf, BUF_LEN, fp1) != NULL)) {
p = buf;
//printf("%s\n", p);
switch(*p) {
case '@':
if (line_num % 2 == 0) {
line_num++;
continue;
}
memcpy(tmp_buf, p+5, 11);
tmp_buf[11] = '\0';
printf("%s\n", tmp_buf);
break;
case '*':
memcpy(tmp_buf, p+15, 2);
tmp_buf[2] = '\0';
printf("%s\n", tmp_buf);
break;
default:
break;
}
line_num++;
}
printf("Total line: %d\n", line_num);
return 0;
}
阅读(3021) | 评论(0) | 转发(0) |