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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值