C语言文件操作 和 预编译命令

//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)宏的使用一定要细心,需要把参数小心的用括号括起来,
* 因为宏只是简单的文本替换,不注意,容易引起歧义错误。
*/

你可能感兴趣的:(NDK)