当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C语言学习之联合体_枚举_IO实例

C语言学习之联合体_枚举_IO实例

2018年09月14日  | 移动技术网IT编程  | 我要评论

游戏推广员,蛇人帝国,帝飞影视

c语言学习之联合体_枚举_io实例

#define _crt_secure_no_warnings

#include
#include

#include
//联合体(共用体)
//不同类型的变量共同占用一段内存(相互覆盖),联合变量任何时刻只有一个成员存在,节省内存
//联合体变量的大小=最大的成员所占的字节数
/*union myvalue
{
 int x;
 double y;
};

void main(){
 union myvalue value;
 value.x= 20;
 value.y = 78.9;
 //第一个值被覆盖了
 printf("%d,%lf\n",value.x,value.y);
 getchar();
}*/

/*typedef union jvalue {
 jboolean z;
 jbyte b;
 jchar c;
 jshorts;
 jint  i;
 jlong j;
 jfloatf;
 jdouble  d;
 jobject  l;
} jvalue;*/
//枚举(列举所有的情况)
//限定值,保证取值的安全性
/*enum day
{
 monday,
 tuesday,
 wednesday,
 thursday,
 friday,
 saturday,
 sunday
};

void main(){
 //枚举的值必须是括号的值
 enum day day = monday;
 //值为0
 printf("%#x,%d \n",&day,day);
 getchar();
}*/
// 读取文本文件
/*void main(){
 //文件路径
 char* path = "d:\\files\\friends.txt";
 //打开  第一个参数地址,第二个参数mode
 file * fp=fopen(path, "r");
 if (fp==null)
 {
  printf("文件读取失败!");
  return;
 }
 //读取
 char buf[50];
 // char * fgets(char * _buf,file * _file)
 while (fgets(buf,50,fp)){
  printf("%s",buf);
 }
 //关闭流
 fclose(fp);

 system("pause");
 getchar();
}*/
//写入文件
/*void main(){
 char* path = "d:\\files\\friends_new.txt";
 //打开文件
 file * fp=fopen(path, "w");
 //写入的文本
 char * text = "写入的内容.";
 //写入文本
 fputs(text,fp);
 //关闭流
 fclose(fp);
 getchar();
}*/
//计算机的文件存储在物理上都是二进制
//文本文件和二进制之分,其实是一个逻辑之分
//c读写文本文件与二进制文件的差别仅仅体现在回车换行符
//写文本时,每遇到一个'\n',会将其转换成'\r\n'(回车换行)
//读文本时,每遇到一个'\r\n',会将其转换成'\n'

//文件复制
/*void main(){
 //文件的原地址和文件复制后的地址
 char* src_path = "d:\\files\\liuyan.png";
 char* new_path = "d:\\files\\liuyan_new.png";
 //打开文件
 file* src_fp=fopen(src_path, "rb");
 file* new_fp = fopen(new_path, "wb");
 //复制
 int buf[50];//缓存
 int len = 0;//每次读取的长度
 while ((len = fread(buf, sizeof(int), 50, src_fp)) != 0){
  //将读到的内容写到新的文件
  fwrite(buf, sizeof(int), len, new_fp);
 }
 //关闭流
 fclose(src_fp);
 fclose(new_fp);

 getchar();
}*/
//查看文件大小
/*void main(){
 //获取文件地址的file指针
 char* src_path = "d:\\files\\liuyan.png";
 file* src_fp = fopen(src_path, "rb");
 //seek_end文件末尾,0偏移量
 fseek(src_fp, 0, seek_end);
 //返回当前的文件指针,相对于文件开头的位移量
 long file_size=ftell(src_fp);
 printf("%ld", file_size);
 getchar();
}*/
//练习文件的加密和解密

//加密,
//规则用到异或规则,第一次异或成其它数,其它数再次和同一个数异或则又为这个数
//如 3 ^ 5 = 0011(3) ^ 0101(5) = 0110(6)
// 6 ^ 5 =0110(6) ^ 0101(5) =0011(3)  可以看到3 经过两次异或又为自己
//规则:1 ^ 1 = 0, 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1 同为0,不同为1

/*void crpypt(char* src_path, char* crpy_path){
 //打开文件
 file* src_fp = fopen(src_path, "r");
 file* new_fp = fopen(crpy_path, "w");
 //加密
 int ch;
 while ((ch = fgetc(src_fp)) != eof){
  fputc(ch ^ 9, new_fp);
 }
 //关闭
 fclose(src_fp);
 fclose(new_fp);
}

//解密
void decrpypt(char* crpy_path, char* decr_path){
 //打开文件
 file* src_fp = fopen(crpy_path, "r");
 file* new_fp = fopen(decr_path, "w");
 //加密
 int ch;
 while ((ch = fgetc(src_fp)) != eof){
  fputc(ch ^ 9, new_fp);
 }
 //关闭
 fclose(src_fp);
 fclose(new_fp);
}

void main(){
 char* src_path = "d:\\files\\friends.txt";
 char* crpy_path = "d:\\files\\crpy_friends.txt";
 char* decr_path = "d:\\files\\decr_friends.txt";
 //加密
 crpypt(src_path, crpy_path);
 //解密
 decrpypt(crpy_path, decr_path);
 getchar();
}*/
//二进制文件加解密
//读取二进制文件中的数据时,一个一个字符读取
//密码:ilovely
void crpypt(char* normal_path, char* crypt_path, char* password){
 //打开文件
 file *normal_fp = fopen(normal_path, "rb");
 file *crypt_fp = fopen(crypt_path, "wb");
 //一次读取一个字符
 int ch;
 int i = 0; //循环使用密码中的字母进行异或运算
 int pwd_len = strlen(password); //密码的长度
 while ((ch = fgetc(normal_fp)) != eof){ //end of file
  //写入(异或运算)
  fputc(ch ^ password[i % pwd_len], crypt_fp);
  i++;
 }
 //关闭
 fclose(crypt_fp);
 fclose(normal_fp);
}

//解密
void decrpypt(char* crypt_path, char* decrypt_path, char* password){
 //打开文件
 file *normal_fp = fopen(crypt_path, "rb");
 file *crypt_fp = fopen(decrypt_path, "wb");
 //一次读取一个字符
 int ch;
 int i = 0; //循环使用密码中的字母进行异或运算
 int pwd_len = strlen(password); //密码的长度
 while ((ch = fgetc(normal_fp)) != eof){ //end of file
  //写入(异或运算)
  fputc(ch ^ password[i % pwd_len], crypt_fp);
  i++;
 }
 //关闭
 fclose(crypt_fp);
 fclose(normal_fp);

}
void main(){
 char* src_path = "d:\\files\\liuyan.png";
 char* crpy_path = "d:\\files\\crpy_liuyan.png";
 char* decr_path = "d:\\files\\decr_liuyan.png";
 //加密
 crpypt(src_path, crpy_path,"i love you");
 //解密
 decrpypt(crpy_path, decr_path,"i love you");
 getchar();
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网