王学岗文本/二进制的读取和加密解密

来源:互联网 发布:游戏编程培训学校 编辑:程序博客网 时间:2024/06/08 17:57
#include <stdio.h>#include <stdlib.h>#include <string.h>//内容一:读取文件-文本文件// int main(){//     //文件的路径//     //Windows下:F:\\test.txt//     //Mac下:/Users/yangshaohong/Desktop/test.txt//     char file_path[] = "/Users/yangshaohong/Desktop/test.txt";//     //打开文件(和Java类似)//     //参数一:文件路径//     //参数二:文件权限(例如:r代表读  w代表写  rw读写)//     //返回值:文件指针//     FILE *file = fopen(file_path,"r");//     //判断一下文件是否为空(Java里面  obj == null)//     if(file == NULL){//         printf("当前文件不存在.....");//         return 0;//     }//     //存在-读取内容//     //定义缓冲区(C++、Java、OC、Swift、ASP.NET等等......)//     char buffer[100];//     //参数一:缓冲区//     //参数二:每次读取长度(注意:不能够超过缓冲区的大小)//     //参数三:源文件//     //返回值:字符指针(如果返回'\0',否则存在)//     while(fgets(buffer,100,file)){//         //打印//         //注意:不需要加换行符//         printf("%s",buffer);//     }// fclose(file)//     getchar();//     return 0;// }//内容二:写入文件-文本文件// int main(){//     //写文件的路径//     char file_path[] = "/Users/yangshaohong/Desktop/test_new.txt";//     //首先需要打开文件//     FILE *file = fopen(file_path,"w");//     //字符串(字符数组)//     char content[] = "I have a Dream.";//     //参数一:写入的内容//     //参数二:源文件//     fputs(content,file);//     //关闭流//     fclose(file);//     getchar();//     return 0;// }//内容三:二进制文件读写//计算机中分为了二进制文件和文本文件(逻辑上面区分)//对于计算机本身而言都是二进制//二进制文件和文本文件区别?//答:就在于换行符不一样//文本文件://写入文件:每当遇到'\n',会将其转成'\r\n'(回车换行)//读取文本:每当遇到'\r\n',就会将其转成'\n'//二进制文件:复制// int main(){//     //将文件的内容写到另外一个文件中(边读边写)//     //定义源文件//     char old_file_path[] = "/Users/yangshaohong/Desktop/test.txt";//     //定义目标文件//     char new_file_path[] = "/Users/yangshaohong/Desktop/test_new.txt";//     //打开源文件//     //rb:代表读取二进制文件//     FILE *old_file_p = fopen(old_file_path,"rb");//     //打开目标文件//     //wb:代表写入二进制文件//     FILE *new_file_p = fopen(new_file_path,"wb");//     //边读边写//     //定义缓冲区//     int buffer[50];//     //记得我们在Java中读取文件的时候,我们是不是要定义一个读取多长//     //C语言和Java一样//     int len = 0;//     //参数一:缓冲区//     //参数二:单位大小//     //参数三:每次读取的长度//     //参数四:源文件//     while((len = fread(buffer,sizeof(int),50,old_file_p))!=0){//         //写入到目标文件//         //参数一:缓冲区//         //参数二:单位大小//         //参数三:每次读取的长度//         //参数四:目标文件//         //例如:文件大小100,缓冲区60,第一次读取:len = 60  第二次:len = 40//         fwrite(buffer,sizeof(int),len,new_file_p);//     }//     //关闭流//     fclose(new_file_p);//     fclose(old_file_p);//     getchar();//     return 0;// }//内容四:获取文件大小// int main(){//     //定义源文件(54 字节)//     char old_file_path[] = "/Users/yangshaohong/Desktop/test.txt";//     //打开文件//     FILE *file = fopen(old_file_path,"r");//     //在C语言中可以将指针指向最后一个字节//     //通过一个函数,将文件指针移动到文件的最后一个位置//     //参数一:源文件//     //参数二:偏移量(倒数第几个)//     //参数三:位置标记(SEEK_END:表示文件指针移动到最后一个位置)//     //一旦调用fseek,指针立马移动到指定的位置//     fseek(file,0,SEEK_END);//     //获取文件的大小:ftell方法(当前文件指针指向位置)//     long fileSize = ftell(file);//     printf("文件大小: %ld\n",fileSize);//     getchar();//     return 0;// }//内容五:文件加密(加密算法可以自定义)-文本文件//分析算法:异或运算(^)//假设:a = 5  b = 6//a二进制:101//b二进制:110//异或运算规则:相同为0,不同为1//a^b:异或运算 011// int main(){//     //源文件//     char source_file_path[] = "/Users/yangshaohong/Desktop/test.txt";//     //加密文件//     char encpytion_file_path[] = "/Users/yangshaohong/Desktop/test_en.txt";//     //打开源文件//     FILE *source_p = fopen(source_file_path,"r");//     //打开加密文件//     FILE *encpytion_p = fopen(encpytion_file_path,"w");//     //通过循环一个个字符读取,然后给每一个字节进行加密算法处理//     int ch;//     //参数一:源文件//     //返回值:当前读取的位置---EOF: End of file(文件读取结束)//     while((ch = fgetc(source_p)) != EOF){//         //给单个字符进行异或运算//         fputc(ch^6,encpytion_p);//     }//     //关闭流//     fclose(encpytion_p);//     fclose(source_p);//     getchar();//     return 0;// }//内容五:文件解密(加密算法可以自定义)-文本文件//分析:异或运算(^)//假设:a = 5  b = 6//a二进制:101//b二进制:110//异或运算规则:相同为0,不同为1//a^b:异或运算 011 = 3 = c//c^b:异或运算//c二进制:011//b二进制:110//结果:   101 = a(还原了)// int main(){//     //加密文件//     char encpytion_file_path[] = "/Users/yangshaohong/Desktop/test_en.txt";//     //解密文件//     char decpytion_file_path[] = "/Users/yangshaohong/Desktop/test_de.txt";//     //打加密文件//     FILE *encpytion_p = fopen(encpytion_file_path,"r");//     //打开解密文件//     FILE *decpytion_p = fopen(decpytion_file_path,"w");//     //通过循环一个个字符读取,然后给每一个字节进行加密算法处理//     int ch;//     //参数一:源文件//     //返回值:当前读取的位置---EOF: End of file(文件读取结束)//     while((ch = fgetc(encpytion_p)) != EOF){//         //给单个字符进行异或运算//         fputc(ch^6,decpytion_p);//     }//     //关闭流//     fclose(decpytion_p);//     fclose(encpytion_p);//     getchar();//     return 0;// }// //内容六:文件加密-二进制文件(通过密码来加密)// int main(){//     //源文件//     char source_file_path[] = "/Users/yangshaohong/Desktop/icon_image.gif";//     //加密文件//     char encpytion_file_path[] = "/Users/yangshaohong/Desktop/icon_image_en.gif";//     //打开源文件//     FILE *source_p = fopen(source_file_path,"rb");//     //打开加密文件//     FILE *encpytion_p = fopen(encpytion_file_path,"wb");//     //密码(我们要根据密码进行加密)//     char pwd[] = "Dream";//     //通过循环一个个字符读取,然后给每一个字节进行加密算法处理//     int ch;//     int i = 0;//     //数组长度//     int pwdLen  = strlen(pwd);//     //参数一:源文件//     //返回值:当前读取的位置---EOF: End of file(文件读取结束)//     while((ch = fgetc(source_p)) != EOF){//         //给单个字符进行异或运算//         fputc(ch^pwd[i%pwdLen],encpytion_p);//         i++;//     }//     //关闭流//     fclose(encpytion_p);//     fclose(source_p);//     getchar();//     return 0;// }//内容六:文件解密-二进制文件(通过密码来解密)// int main(){//     //密码(我们要根据密码进行加密)//     char pwd[5];//     printf("请输入解密密码:");//     scanf("%s",pwd);//     //加密文件//     char encpytion_file_path[] = "/Users/yangshaohong/Desktop/icon_image_en.gif";//     //解密文件//     char decpytion_file_path[] = "/Users/yangshaohong/Desktop/icon_image_de.gif";//     //打加密文件//     FILE *encpytion_p = fopen(encpytion_file_path,"rb");//     //打开解密文件//     FILE *decpytion_p = fopen(decpytion_file_path,"wb");//     //通过循环一个个字符读取,然后给每一个字节进行加密算法处理//     int ch;//     int i = 0;//     //数组长度//     int pwdLen  = strlen(pwd);//     //参数一:源文件//     //返回值:当前读取的位置---EOF: End of file(文件读取结束)//     while((ch = fgetc(encpytion_p)) != EOF){//         //给单个字符进行异或运算//         fputc(ch^pwd[i%pwdLen],decpytion_p);//         i++;//     }//     //关闭流//     fclose(decpytion_p);//     fclose(encpytion_p);//     getchar();//     return 0;// }
0 0