c_day05_io

来源:互联网 发布:软件购买合同 编辑:程序博客网 时间:2024/06/05 03:57
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include "string.h"
/*
//读取文本内容
void main(){


char path[] = "C:/Users/Administrator/Desktop/files/test.txt";
  //打开文本


FILE *fp = fopen(path, "r");
if (fp == NULL){
printf("文件获取失败!");
return;
      }


//读取
char buff[50];   //创建缓冲区
while (fgets(buff,50,fp)){
printf("%s",buff);
}
fclose(fp);


getchar();
}
*/
/*
//向文本中写入内容
void main(){
//创建输出路径
char path[] = "C:/Users/Administrator/Desktop/files/test_new.txt";
FILE *fp = fopen(path,"w");
char *msg = "我要在计划期内完成学习任务,这样才对的起自己的计划!\n 人没有梦想和咸鱼有什么区别!";
fputs(msg,fp);
//关闭流
fclose(fp);
getchar();
}
*/
/*
//复制非文本文件 eg: jpg图片复制
void main(){
char* path_r = "C:/Users/Administrator/Desktop/files/beauty.jpg";
char* path_w = "C:/Users/Administrator/Desktop/beauty_new.jpg";
//读取二进制文件 b字符表示朝族二进制的文件bianry
FILE* fr = fopen(path_r,"rb");
//写入
FILE* fw = fopen(path_w, "wb");
//复制文件
int buff[50]; //缓冲区
int len = 0; //每次读取的长度


while (len = fread(buff,sizeof(int),50,fr)){
  //将读到的内容写入新的文件中
fwrite(buff,sizeof(int),len,fw);
}


//关闭流
fclose(fr);
fclose(fw);
getchar();
}*/


/*


//获取文件的大小
void main(){
char* path_r = "C:/Users/Administrator/Desktop/files/beauty.jpg";
FILE *fp = fopen(path_r,"r");
//重新定位文件指针
//SEEK_END文件末尾,0偏移量
fseek(fp,0,SEEK_END);
//返回当前文建制镇,相对于开头的位置偏移量
long file_size = ftell(fp);
printf("%d\n",file_size/1024);
getchar();
}*/


/*
//exercise 文本文件加密/解密
//异或运算  规则 1^1=0 0^0=1 0^1 =1;同为0不同为1;
void crpypt(char* normal_path,char* crypt_path){
//打开文件
FILE *fp_r = fopen(normal_path, "r");
FILE *fp_w = fopen(crypt_path, "w");
//一次读取一个字符
int ch;
while ((ch = fgetc(fp_r))!=EOF){  //End of file -》EOF = -1
    //加密后写入文件
//(异或运算)
fputc(ch^8,fp_w);
}


fclose(fp_w);
fclose(fp_r);


}
void main(){
char* path_r = "C:/Users/Administrator/Desktop/files/test.txt";
char* path_w = "C:/Users/Administrator/Desktop/text_new.txt";      //^8
char* path_w2 = "C:/Users/Administrator/Desktop/text_new_dec.txt";  //^8  对同一个数字异或运算两次等于不变
crpypt(path_w,path_w2);
getchar();
}
*/




//exercise 非文件加密/解密
//密码:ilovelyf
void crpypt(char* normal_path, char* crypt_path,char password[]){
//打开文件
FILE *fp_r = fopen(normal_path, "rb");
FILE *fp_w = fopen(crypt_path, "wb");
//一次读取一个字符
int ch;
while ((ch = fgetc(fp_r)) != EOF){  //End of file -》EOF = -1
//加密后写入文件
//(异或运算)
int i = 0;
int pass_len = strlen(password);
fputc(ch ^ password[i%pass_len], fp_w);
i++;
}


fclose(fp_w);
fclose(fp_r);


}
void main(){
char* path_r = "C:/Users/Administrator/Desktop/files/beauty.jpg";
char* path_w = "C:/Users/Administrator/Desktop/beauty_new.jpg";      //^8
char* path_w2 = "C:/Users/Administrator/Desktop/beauty_dec.jpg";  //^8  对同一个数字异或运算两次等于不变
char* password = "ilovelyf";
//加密
crpypt(path_r, path_w,password);
//解密
crpypt(path_w, path_w2, password);
getchar();
}
原创粉丝点击