Io的流简单操作及预处理命令使用教程
//Linux基于文件,影视屏 加密都是存在本地 //键盘浏览器什么都是文件 //c语言读文件 int main(){ //c语言中地址要用\\ 才能获取到 char *path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\friends.txt"; FILE *fp=fopen(path,"r"); char buff[50]; while (fgets(buff,50,fp)) { printf("%s", buff); } fclose(fp); system("pause"); return 0; } ========写文件===== int main(){ char *path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\write.txt"; FILE *fp = fopen(path, "w"); if (fp == NULL) { printf("返回失败"); } char *text = "动脑学院,看好你!"; fputs(text, fp); fclose(fp); system("pause"); return 0; } =====二进制存储的===== //文件都是以二进制存储的 //文件分为两部分:控制信息和内容信息 //读文件:磁盘>>文件缓冲区>>应用程序内存空间 //根据控制信息相应的编解码 //C语言 二进制文件和文本文件读写区别 // 写文本 "\n" 替换成 “\r\n" //读文本 "\r\n" 替换成 “\n" int main(){ char *read_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\LogViewPro_write.exe"; char *wirte_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\wite_path.exe"; //read FILE *read_fp = fopen(read_path, "rb"); //wite FILE *write_fp = fopen(wirte_path, "wb"); char buff[50]; int len = 0; while ((len = fread(buff, sizeof(char), 50, read_fp))!=0){ fwrite(buff, sizeof(char), len, write_fp); } fclose(read_fp); fclose(write_fp); system("pause"); return 0; } =======/读一个文件的大小===== int main(){ char *read_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\liuyan.png"; FILE *fp = fopen(read_path, "r"); if (fp==NULL) { return 0; } //文件指针偏移量 fseek(fp,0,SEEK_END); long filesize = ftell(fp); printf("%ld \n", filesize); system("pause"); return 0; } ====文件加解密 简单的可逆的加密====== void encode(char normal_path[], char encode_path[]){ FILE *normal_fp = fopen(normal_path, "r"); FILE *encode_fp = fopen(encode_path, "w"); int ch; //文件系统可以用-1 可以用eof 代表文件结束 while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch^7,encode_fp); } fclose(normal_fp); fclose(encode_fp); } //文件解密 void dncode(char normal_path[], char encode_path[]){ FILE *normal_fp = fopen(normal_path, "r"); FILE *encode_fp = fopen(encode_path, "w"); int ch; //文件系统可以用-1 可以用eof 代表文件结束 while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch ^ 7, encode_fp); } fclose(normal_fp); fclose(encode_fp); } int main(){ char *normal_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\friends.txt"; char *encode_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\encode.txt"; char *dncode_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\dncode.txt"; //encode(normal_path,encode_path); 加密 dncode(encode_path, dncode_path); system("pause"); return 0; } =======二进制的加解密======= void encode(char normal_path[], char encode_path[],char *password){ FILE *normal_fp = fopen(normal_path, "rb"); FILE *encode_fp = fopen(encode_path, "wb"); int ch; int leght = strlen(password); int i = 0; //文件系统可以用-1 可以用eof 代表文件结束 while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch^password[i%leght],encode_fp); i = (i++) % 1001; } fclose(normal_fp); fclose(encode_fp); } void decode(char encode_path[], char dncode_path[], char *password){ FILE *normal_fp = fopen(encode_path, "rb"); FILE *encode_fp = fopen(dncode_path, "wb"); int ch; int leght = strlen(password); int i = 0; //文件系统可以用-1 可以用eof 代表文件结束 while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch^password[i%leght], encode_fp); i = (i++) % 1001; } fclose(normal_fp); fclose(encode_fp); } int main(){ char *normal_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\liuyan.png"; char *encode_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\liuyan_encode.png"; char *dncode_path = "D:\\BaiduNetdiskDownload\\ndk\\Lsn5_C语言文件操作 和 预编译命令_2017-8-2\\files\\liuyan_decode.png"; //encode(normal_path, encode_path,"ILoveDn"); decode(encode_path, dncode_path, "ILoveDn"); system("pause"); return 0; } =======预处理命令 预处理命令============================= // <> 和 "" 的区别 <> 是系统提供的 "" 是自己编译的 .h 头文件 //具体 #define NUM 5 #define MAX(x,y)((x)>(y))?x:y //简单预处理 宏定义 //define 标识符 字符串 字符替换 //typedef 别名 #define M int main(){ #include "a.txt" for (int i = 0; i < NUM; i++) { printf("%d \n", i); } int a = 10, b = 12; int h=MAX(a, b); printf("%d \n", h); //定义了 #ifdef M printf("11101\n"); #else printf("102\n"); #endif //如果没有定义 #ifndef K printf("10003\n"); #endif system("pause"); return 0; }