C語言檔案操作 和 預編譯命令
阿新 • • 發佈:2018-11-16
//read檔案 int main(){ char *path = "D:\\friends.txt"; FILE *fp = fopen(path, "r"); char buff[500]; while (fgets(buff,50,fp)){ printf("%s\n", buff); } fclose(fp); system("pause"); return 0; } //寫檔案 int main(){ char *path = "D:\\friends.txt"; FILE * fp = fopen(path, "w"); if (fp == NULL){ printf("failed。。。。"); return 0; } char * text = "我們看好你"; fputs(text, fp); fclose(fp); system("pause"); return 0; } //讀寫二進位制檔案 int main() { char * read_path = "D:\\LogViewPro.exe"; char * write_path = "D:\\LogViewPro_write.exe"; //read FILE * read_fp = fopen(read_path, "rb"); //write FILE * write_fp = fopen(write_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; } //size of the file int main() { char *read_path = "D:\\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; while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch ^ 7, encode_fp); } fclose(normal_fp); fclose(encode_fp); } void decode(char encode_path[], char decode_path[]) { FILE * normal_fp = fopen(encode_path, "r"); FILE * encode_fp = fopen(decode_path, "w"); int ch; while ((ch = fgetc(normal_fp)) != EOF) { fputc(ch ^ 7, encode_fp); } fclose(normal_fp); fclose(encode_fp); } int main() { char * nomal_path = "D:\\friends.txt"; char * encode_path = "D:\\friends_encode.txt"; char * decode_path = "D:\\friends_decode.txt"; encode(nomal_path, encode_path); decode(encode_path, decode_path); system("pause"); return 0; } /////////////////////////////////////////二進位制的加解密/////////////////////////////////////// void biEncode(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 pwd_legth = strlen(password); int i = 0; while ((ch = fgetc(normal_fp)) != EOF){ fputc(ch ^ password[i % pwd_legth], encode_fp); i = (i++) % 10001; } fclose(normal_fp); fclose(encode_fp); } void biDecode(char encode_path[], char decode_path[], char * password) { FILE * normal_fp = fopen(encode_path, "rb"); FILE * encode_fp = fopen(decode_path, "wb"); int ch; int pwd_legth = strlen(password); int i = 0; while ((ch = fgetc(normal_fp)) != EOF) { fputc(ch ^ password[i % pwd_legth], encode_fp); i = (i++) % 10001; } fclose(normal_fp); fclose(encode_fp); } int main() { char * nomal_path = "D:\\liuyan.png"; char * encode_path = "D:\\liuyan_encode.png"; char * decode_path = "D:\\liuyan_decode_.png"; biEncode(nomal_path, encode_path, "ILoveDn"); biDecode(encode_path, decode_path, "ILoveDn"); //encode(nomal_path, encode_path); /*decode(encode_path, decode_path);*/ system("pause"); return 0; } //#define 識別符號 字串 // 字串替換 // typedef 別名 #define MAX(x, y) ((x) > (y)) ? x: y #define M int main() { //#include "A.txt" for (int i = 0; i < 5; i++) { printf("%d \n", i); } int max = MAX(3, 5); printf("%d \n", max); #ifdef M #ifdef M printf("%d \n", 110); #else printf("%d \n", 120); #endif #endif #ifndef X printf("%d \n", 130); #endif system("pause"); return 0; } #define MAX(x,y) (((x)>(y))?(x):(y)) #define MIN(x,y) (((x)<(y))?(x):(y)) int main(void) { #ifdef MAX //判斷這個巨集是否被定義 printf("3 and 5 the max is:%d\n", MAX(3, 5)); #endif #ifdef MIN printf("3 and 5 the min is:%d\n", MIN(3, 5)); #endif return 0; } /* * (1)三元運算子要比if,else效率高 * (2)巨集的使用一定要細心,需要把引數小心的用括號括起來, * 因為巨集只是簡單的文字替換,不注意,容易引起歧義錯誤。 */