摘要:本文将带你了解C语言学习之联合体_枚举_IO实例,希望本文对大家学C语言有所帮助。
本文将带你了解C语言学习之联合体_枚举_IO实例,希望本文对大家学C语言有所帮助。
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();
}
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号