#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[]) //指针数组作为主函数参数,用于函数带参输入
{
//定义并初始化变量
FILE *fp;
char ch;
unsigned char chrl,chrh;
unsigned int count=0,checksum=0,chr=0;
unsigned long int sum=0;
//打开文件 argv[1]
if((fp=fopen(argv[1],"r"))==NULL)
{
printf("\n\n File can't be opened");
exit(1);
}
printf("\n\nthe type of output:data---sum\n\n");
//从txt文件取读字符,并进行数据处理
while(1)
{
if((ch=fgetc(fp))!=EOF)
{
count;
if(ch!=' ')
{
if(count%12==0)
printf("\n");
//将字符转换成相应的整型变量
if(ch>='0'&&ch<='9')
ch-='0';
else
if(ch>='a'&&ch<='f')
ch=ch-'a'+10;
else
if(ch>='A'&&ch<='F')
ch=ch-'A'+10;
//计算8字节数的累加值,将奇数位累加到长整型sum的15-8位,偶数位累加到低8-0位,
//这样就避免了数据个数为奇偶的处理操作
if(count%2==1)
chrh=ch<<4;
else
{
chrl=ch&0x0f;
chr=chrh|chrl;
if(count%4==2)
sum+=ch<<8;
else
if(count%4==0)
sum+=chr;
printf("%x--%lx ",chr,sum);
}
}
else
{
count--;
}
}
else
{
break;
}
}
//将sum由32位折合成16位
if(sum>>16)
checksum=~(long(sum>>16)+long(sum&0x0000ffff));
checksum=checksum&0x0000ffff;
//结果输出
printf("\n\nsum:%lx----checksum:%x",sum,checksum);
printf("\n\nsource:argc=%d,\targv=%s\n",argc,argv[1]);
fclose(fp);
}
警告信息为:File:fopen.c
line:53
Expression: file!=NULL
參考答案:第一个参数是argv[0],不是argv[1]