#define _CRT_SECURE_NO_WARNINGS//关闭安全检查 #include<stdio.h> #include<stdlib.h> int getfilesize(char *path) { FILE *pf = fopen(path, "r"); if (pf == NULL) { return -1; } else { fseek(pf, 0, SEEK_END); int length = ftell(pf); return length;//获取文件大小 } } void copy(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } //加密 void jia(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 for (int i = 0; i < length; i++) { p[i] ^= 'A'; } fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } //解密 void jie(char *oldpath, char *newpath) { FILE *pfr, *pfw; pfr = fopen(oldpath, "rb"); pfw = fopen(newpath, "wb");//写入二进制模式 if (pfr == NULL || pfw == NULL) { fclose(pfr);//关闭文件 fclose(pfw); return; } else { int length = getfilesize(oldpath); char *p = (char *)malloc(length*sizeof(char));//分配内存,读取文件 fread(p, sizeof(char), length, pfr);//读取二进制到内存 for (int i = 0; i < length; i++) { p[i] ^= 'A'; } fwrite(p, sizeof(char), length, pfw);//写入二进制到文件 fclose(pfr);//关闭文件 fclose(pfw); } } void main() { char *oldpath = "C:\\Users\\yincheng01\\Desktop\\腾讯截图2012.exe"; char *newpath = "C:\\Users\\yincheng01\\Desktop\\腾讯截图2012A.exe"; char *newjiepath = "C:\\Users\\yincheng01\\Desktop\\腾讯截图2012B.exe"; jia(oldpath, newpath); jie(newpath, newjiepath); system("pause"); }
fseek
函数名: fseek
功 能: 重定位流上的文件指针
用 法: int fseek(FILE *stream, long offset, int fromwhere);
描 述: 函数设置文件指针stream的位置。如果执行成功,stream将指向以fromwhere为基准,偏移offset个字 节的位置。如果执行失败(比如offset超过文件自身大小),则不改变stream指向的位置。
返回值: 成功,返回0,否则返回其他值。
fseek position the file position pointer for the file referenced by stream to the byte location calculated by offset.
ftell
函数 ftell 用于得到文件位置指针当前位置相对于文件首的偏移字节数