#include
#include
#include
typedef struct Link
{
char name[5];
char gender[5];
int tail;
}link;
int main(int argc, const char *argv[])
{
//定义文件指针可写
FILE *p=NULL;
if((p=fopen(argv[1],"w"))==NULL)
{
perror("fopen error");
return -1;
}
if(argc!=3)
{
fputs("input file error\n",stdout);
fputs("usage:./a.out srcfile\n",stderr);
return -1;
}
link s[2]={{"zwt","m",182},{"cjj","w",163}};
fwrite(s,sizeof(link),2,p);
fclose(p);
//定义文件指针可写
if((p=fopen(argv[1],"r"))==NULL)
{
perror("fopen error");
return -1;
}
//定义文件指针可读
FILE *q=NULL;
if((q=fopen(argv[2],"w"))==NULL)
{
perror("fopen error");
return -1;
}
while(1)
{
link t;
int rew;
//读取p文件中的值
rew=fread(&t,sizeof(t),1,p);
if(rew<1)
{
break;
fclose(q);
fclose(p);
}
//拷贝到q argv[2]中
fwrite(&t,sizeof(link),1,q);
}
return 0;
}
ubuntu
#include
#include
#include
int register_zc();
int login_dl();
int main(int argc, const char *argv[])
{
char n;
while(1)
{
printf("\t\t=========0、退出======\n");
printf("\t\t=========1、登录======\n");
printf("\t\t=========2、注册======\n");
printf("请输入功能选项:");
scanf("%c",&n);
while(getchar()!='\n');
switch(n)
{
case '0':
{
exit(0);
}
case '1':
{
login_dl();
break;
}
case '2':
{
register_zc();
break;
}
default:printf("您输入功能选项有误\n");
}
printf("请输入任意键按回车清屏!!!\n");
while(getchar()!='\n');
system("clear");
}
return 0;
}
int register_zc()
{
char id[20];
char password[20];
printf("请输入要注册的账号:");
fgets(id,sizeof(id),stdin);
printf("请输入要注册的密码:");
fgets(password,sizeof(password),stdin);
//将账号密码的换行符取消,存放在同一行
id[strlen(id)-1]='\0';
password[strlen(password)-1]='\0';
//定义文件指针
FILE*p=NULL;
if((p=fopen("./user.txt","a+"))==NULL)
{
perror("fopen error");
return -1;
}
fprintf(p,"%s %s\n",id,password);
fclose(p);
printf("注册成功\n");
return 0;
}
int login_dl()
{
char id[20],password[20];//注册过的账号密码
char id_log[20],password_log[20];//登录的账号密码
printf("请输入你要登录的账号:");
fgets(id_log,sizeof(id_log),stdin);
printf("请输入你要登录的密码:");
fgets(password_log,sizeof(password_log),stdin);
FILE*q=NULL;
if((q=fopen("./user.txt","a+"))==NULL)
{
perror("fopen error");
return -1;
}
id_log[strlen(id_log)-1]='\0';
password_log[strlen(password_log)-1]='\0';
while(1)
{
int rew;
rew=fscanf(q,"%s %s",id,password);
if(rew<0)
{
printf("登录失败\n");
fclose(q);
return -1;
}
if(strcmp(id,id_log)==0&&strcmp(password,password_log)==0)
{
printf("登录成功!\n");
fclose(q);
return 0;
}
}
}
#include
#include
#include
int main(int argc, const char *argv[])
{
FILE *fp=NULL;
if((fp=fopen("./ggb.bmp","r+"))==NULL)
{
perror("fopen error");
return -1;
}
//向后偏移两个字节得到文件的大小
fseek(fp,2,SEEK_SET);
unsigned int size;
fread(&size,sizeof(size),1,fp);
printf("size=%d\n",size);
unsigned char color[3]={255,0,0};
fseek(fp,54,SEEK_SET);
for(int i=0;i<250;i++)
{
for(int j=0;j<959;j++)
{
fwrite(color,sizeof(color),1,fp);
}
}
fclose(fp);
return 0;
}