1 读取文件
void main(){
char *path = "E:\\Test\\love.txt";
//打开
FILE *fp = fopen(path,"r");
if (fp==NULL){
printf("文件打开失败");
return;
}
//读取
char buff[50]; //缓冲
while (fgets(buff,50,fp)){
printf("%s",buff);
}
//关闭
fclose(fp);
system("pause");
getchar();
}
打印结果:
在那遥远的地方,有位好姑娘!
2 写入文本文件
void main(){
char *path = "E:\\Test\\love.txt";
//打开
FILE *fp = fopen(path, "w");
char *text = "这街上太拥挤,太多人有秘密。玻璃上有雾气被吹散在房子里";
fputs(text, fp);
//关闭流
fclose(fp);
getchar();
}
3 文件复制
void main(){
char *read_path = "E:\\Test\\liuyan.png";
char *write_path = "E:\\Test\\liuyan_copy.png";
//读取文件 b表示操作二进制文件binary
FILE *read_fp = fopen(read_path,"rb");
//写的文件
FILE *write_fp = fopen(write_path,"wb");
//复制
int buff[50];//缓冲区域
int len = 0;//每次读到的数据长度
while ((len = fread(buff,sizeof(int),50,read_fp))!=0){
//将读到的内容写入新的文件
fwrite(buff,sizeof(int),len,write_fp);
}
//关闭流
fclose(read_fp);
fclose(write_fp);
getchar();
}
4 获取文件大小
void main(){
char *read_path = "E:\\Test\\liuyan.png";
FILE *fp = fopen(read_path, "r");
//重新定位指针
//SEEK_END 文件末尾,0偏移量
fseek(fp, 0, SEEK_END);
//返回当前的文件指针,相对于文件开头的位移量
long filesize = ftell(fp);
printf("%d\n", filesize);
getchar();
}
5 文件加密解密
//加密
void crpypt(char normal_path[], char crypt_path[]){
//打开文件
FILE *normal_fp = fopen(normal_path, "r");
FILE *crypt_fp = fopen(crypt_path, "w");
//一次读取一个字符
int ch;
while ((ch = fgetc(normal_fp)) != EOF){//end of file
//写入(抑或运算)
fputc(ch ^ 9, crypt_fp);
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[]){
//打开文件
FILE *normal_fp = fopen(crypt_path, "r");
FILE *crypt_fp = fopen(decrypt_path, "w");
//一次读取一个字符
int ch;
while ((ch = fgetc(normal_fp)) != EOF){//end of file
//写入(抑或运算)
fputc(ch ^ 9, crypt_fp);
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);
}
void main(){
char *normal_path = "E:\\Test\\love.txt";
char *crypt_path = "E:\\Test\\love_crypt.txt";
char *decrypt_path = "E:\\Test\\love_decrypt.txt";
//加密
//crpypt(normal_path, crypt_path);
//解密
decrpypt(crypt_path,decrypt_path);
getchar();
}
6 文件加密 解密(使用密码)
void crpypt(char normal_path[], char crypt_path[], char password[]){
//打开文件
FILE *normal_fp = fopen(normal_path, "rb");
FILE *crypt_fp = fopen(crypt_path, "wb");
//一次读取一个字符
int ch;
int i = 0; //循环使用密码中的字母进行异或运算
int pwd_len = strlen(password); //密码的长度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//写入(异或运算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);
}
//解密
void decrpypt(char crypt_path[], char decrypt_path[], char password[]){
//打开文件
FILE *normal_fp = fopen(crypt_path, "rb");
FILE *crypt_fp = fopen(decrypt_path, "wb");
//一次读取一个字符
int ch;
int i = 0; //循环使用密码中的字母进行异或运算
int pwd_len = strlen(password); //密码的长度
while ((ch = fgetc(normal_fp)) != EOF){ //End of File
//写入(异或运算)
fputc(ch ^ password[i % pwd_len], crypt_fp);
i++;
}
//关闭
fclose(crypt_fp);
fclose(normal_fp);
}
void main(){
char *normal_path = "E:\\Test\\liuyan.png";
char *crypt_path = "E:\\Test\\crpt_liuyan.png";
char *decrypt_path = "E:\\Test\\decrpt_liuyan.png";
//crpypt(normal_path, crypt_path,"mima");
//解密
decrpypt(crypt_path, decrypt_path, "mima");
getchar();
}
加密解密后: