本地录入数据的时候出错,砥砺,弄好久了
程序代码:
#include <stdio.h> #include <stdlib.h> unsigned char xorTarget;//全局变量声明 unsigned char addTarget; unsigned int orgValue; unsigned char *p = NULL; int n = 0; int i = 0; struct header//页眉结构体(本地保存时写入) { char sav[30]; char SAV[30]; }boy= { "xorTarget\taddTarget\torgValue\n", "你输入的数据如下:\n" }; unsigned char GetXorValue ( unsigned char* pData, int nSize)//xor校验 { unsigned char xorTarget = 0; for ( i = 0; i < nSize; i++ ) xorTarget ^= pData[ i]; return xorTarget; } unsigned char GetAddValue ( unsigned char* pData, int nSize)//add校验 { unsigned int addTarget = 0; for ( i = 0; i < nSize; i++ ) addTarget = ( addTarget + pData [ i] ) &0xff; return ( unsigned char) addTarget; } unsigned int GetCRCValue( unsigned char* pData, int nSize)//crc校验 { int j; unsigned int orgValue = 0xffff , crcConst = 0xa001; for ( i = 0; i< nSize; i++ ) { orgValue ^= ( unsigned int) pData[ i+ 1]; for ( j = 0; j<= 7; j++ ) { if ( 1 == ( orgValue& 1) ) { orgValue >>= 1; orgValue ^= crcConst; } else orgValue >>= 1; } } return orgValue; } void save()//数据保存 { FILE *fp; char file[50]; int flag = 0;//排版标志变量 int N; printf("\n请输入你要保存的数据个数:\n\n"); scanf("%d",&N); printf("\n请输入要保存的地址:\n"); getchar(); gets(file); if((fp=fopen(file,"w"))==NULL) { printf("\n保存失败\n"); getch(); exit(0); } fprintf(fp,"%s\n",boy.SAV);//写入页眉"你输入的数据如下:\n" for(i=0;i<N;i++) { fprintf(fp,"%X\t",*(p+i));//写入用户输入的数据 flag++; if(flag==4) { fprintf(fp,"\n\n");//数据排版 flag = 0; } } fprintf(fp,"\n\n");//空行,用于排版 fprintf(fp,"校验结果如下:\n\n"); fprintf(fp,"%s\n",boy.sav);//写入页眉"xorTarget\taddTarget\torgValue\n" fprintf(fp,"0x%X\t\t0x%X\t\t%u",xorTarget,addTarget,orgValue);//写入校验结果 fclose(fp); printf("\n数据已保存%s中\n",file); } void input() //数据读取函数!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! { FILE *fp; char file[30]; printf("\n请输入你要录入的文件地址:\n"); getchar(); gets(file); if((fp=(file,"r"))==NULL) { printf("\ncannot open\n"); exit(0); } p=(unsigned char*)malloc(sizeof(unsigned char)); fscanf(fp,"%x",p); while(!feof(fp)) { i++; p=(unsigned char* )malloc(sizeof(unsigned char)); fscanf(fp,"%x",p+i); } } void main(void) { start: printf("\t\t\t简单数据校验\n\n");//主菜单 printf("1.从键盘录入数据\n"); printf("2.从本地录入数据\n"); printf("3.异或校验\n"); printf("4.累加和校验\n"); printf("5.循环冗余校验\n"); printf("6.数据保存\n"); printf("7.退出校验\n"); printf("\n请输入你的选择(1~7):"); scanf("%d",&i); if(i<1||i>7) { printf("\n输入错误\n"); goto start; } switch(i) { case 1://数据录入 { system("CLS"); int i; printf("\n请输入你要录入的数据个数:\n"); scanf("%d",&n); printf("\n你要输入%d个数据:\n",n); p=(unsigned char *)malloc(n*sizeof(unsigned char)); if(p==NULL) { printf("\n --is null-- \n"); exit(1); } for(i=0;i<n;i++) scanf("%x",p+i); printf("\n你输入的数据如下:\n\n"); for(i=0;i<n;i++) printf("%x\t",*(p+i)); printf("\n\n"); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 2: { input(); printf("\n你录入的数据如下:\n\n"); for(i=0;i<n;i++) printf("%x\t",*(p+i)); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 3://xor校验 { system("CLS"); xorTarget = GetXorValue(p,n);//函数调用 printf("异或校验结果:0x%X\n",xorTarget); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 4://累加和校验 { system("CLS"); addTarget = GetAddValue(p,n); printf("累加和校验结果:0x%X\n",addTarget); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 5://CRC校验 { system("CLS"); orgValue = GetCRCValue(p,n); printf("循环冗余校验结果:%u\n",orgValue); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 6://保存 { system("CLS"); save(); printf("\n按任意键返回\n"); getch(); system("CLS"); main(); } case 7://退出 { system("CLS"); free(p); printf("\n谢谢使用!\n"); exit(0); } } }
[ 本帖最后由 jianyuling00 于 2012-6-14 22:32 编辑 ]