c_day05_io

#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();
}
發佈了41 篇原創文章 · 獲贊 19 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章