加密解密的C程序有错误,帮忙看看
定义inta=0,u=0,i=0,while1=0,t;charch,str1[512],str2[512],filename[225],file[4096],ch2;F...
定义
int a = 0, u = 0, i = 0, while1 = 0, t; char ch, str1[512], str2[512], filename[225], file[4096], ch2; FILE *fp;
程序片段
printf("1.加密 2.解密\n"); scanf("%d",&a); if(a==1) {printf("输入要加密的文件名来继续:"); scanf("%s",&filename); printf("\n"); printf("正在加密>>>"); fp = fopen(filename, "r"); for (i = 0; i < 4096; i++) file[i] = NULL; i = 0; ch = fgetc(fp); while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; } i = 0; fp = fopen(filename, "w"); fprintf(fp, "%d", file[i]); fclose(fp); fp=fopen(filename,"a"); for (i = 0; i < 4096; i++) fprintf(fp," %d",file[i]); printf("加密完成\n"); } if(a==2) {printf("输入要解密的文件名来继续:"); scanf("%s",&filename); printf("\n"); printf("正在解密>>>"); fp = fopen(filename, "r"); for (i = 0; i < 4096; i++) file[i] = NULL; i = 0; ch = fgetc(fp); while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; } i = 0; fp = fopen(filename, "w"); fprintf(fp, "%c", file[i]); fclose(fp); fp=fopen(filename,"a"); for (i = 0; i < 4096; i++) fprintf(fp,"%c",file[i]); printf("解密完成\n"); } 展开
int a = 0, u = 0, i = 0, while1 = 0, t; char ch, str1[512], str2[512], filename[225], file[4096], ch2; FILE *fp;
程序片段
printf("1.加密 2.解密\n"); scanf("%d",&a); if(a==1) {printf("输入要加密的文件名来继续:"); scanf("%s",&filename); printf("\n"); printf("正在加密>>>"); fp = fopen(filename, "r"); for (i = 0; i < 4096; i++) file[i] = NULL; i = 0; ch = fgetc(fp); while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; } i = 0; fp = fopen(filename, "w"); fprintf(fp, "%d", file[i]); fclose(fp); fp=fopen(filename,"a"); for (i = 0; i < 4096; i++) fprintf(fp," %d",file[i]); printf("加密完成\n"); } if(a==2) {printf("输入要解密的文件名来继续:"); scanf("%s",&filename); printf("\n"); printf("正在解密>>>"); fp = fopen(filename, "r"); for (i = 0; i < 4096; i++) file[i] = NULL; i = 0; ch = fgetc(fp); while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; } i = 0; fp = fopen(filename, "w"); fprintf(fp, "%c", file[i]); fclose(fp); fp=fopen(filename,"a"); for (i = 0; i < 4096; i++) fprintf(fp,"%c",file[i]); printf("解密完成\n"); } 展开
展开全部
错误好几处:
1、while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; }
file[4096],输入的文件超过这个就会数组越界!
2、fp = fopen(filename, "w"); fprintf(fp, "%d", file[i]);
前面打开了还没关闭此处会发生打开失败,另外打开后只输出一个file[i] ?还有file[i]定义是char,此处用%d ?
下面没细看,因排版原因。
建议先参考下别人的程序,自己再慢慢试着写。
1、while (ch != EOF && ch != '烫') { file[i] = ch; ch = fgetc(fp); i++; }
file[4096],输入的文件超过这个就会数组越界!
2、fp = fopen(filename, "w"); fprintf(fp, "%d", file[i]);
前面打开了还没关闭此处会发生打开失败,另外打开后只输出一个file[i] ?还有file[i]定义是char,此处用%d ?
下面没细看,因排版原因。
建议先参考下别人的程序,自己再慢慢试着写。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询