1、实现文件夹拷贝功能
#include <allhead.h>
int main(int argc, const char *argv[])
{
if(argc!=2)
{
printf("参数个数错误!\n");
return -1;
}
char str[256]={0};
char str1[256]={0};
strcpy(str,"./");
strcat(str,argv[1]);
strcpy(str1,str);
//打开要拷贝的文件夹
DIR* dp = opendir(str);
if(dp==NULL);
{
perror("opendir");
if(mkdir(str,S_IRWXU|S_IRWXG|S_IRWXO))
{
perror("mkdir");
return -1;
}
}
closedir(dp);
//创建拷贝文件夹
dp = opendir(str);
if(dp==NULL)
{
perror("opendir");
return -1;
}
struct dirent *attr;
strcat(str1,"_copy");
if(mkdir(str1,S_IRWXU|S_IRWXG|S_IRWXO))
{
perror("mkdir");
return -1;
}
strcat(str,"/");
strcat(str1,"/");
while(1)
{
//读取文件夹中的文件
attr=readdir(dp);
if(attr==NULL)
{break;}
char name[256]={0};
memset(name,0,sizeof(name));
strcpy(name,attr->d_name);
char src[256];
char dest[256];
if(strcmp(name,".")&&strcmp(name,".."))
{
memset(src,0,sizeof(src));
strcat(src,str);
strcat(src,name);
//打开文件
int of=open(src,O_RDONLY);
if(of==-1)
{
perror("open");
return -1;
}
memset(dest,0,sizeof(dest));
strcat(dest,str1);
strcat(dest,name);
int nf=open(dest,O_WRONLY|O_CREAT|O_TRUNC);
if(nf==-1)
{
perror("open");
return -1;
}
//拷贝
while(1)
{
char buf[256];
memset(buf,0,sizeof(buf));
printf("%s",buf);
int res=read(of,buf,sizeof(buf));
if(res==0){break;}
write(nf,buf,res);
}
close(of);
close(nf);
}
}
printf("拷贝成功!\n");
closedir(dp);
return 0;
}
2、学生信息
#include <allhead.h>
typedef struct Student{
char name[20];
int age;
double math_score;
double chinese_score;
double english_score;
double physical_score;
double chemical_score;
double biological_score;
}stu_t;
int main(int argc, const char *argv[])
{
stu_t stu[100] = {0};
printf("----------学生信息管理----------\n");
printf("1、学生信息录入\n");
printf("2、学生信息保存\n");
printf("3、学生信息读取\n");
printf("4、学生信息删除\n");
printf("5、退出\n");
while(1)
{
int sec;
int i=0;
printf("请输入您要使用的功能:");
scanf("%d",&sec);
while(getchar()!=10);
switch(sec)
{
case 1:
while(i<2)
{
printf("姓名:");
scanf("%s",&stu[i].name);
printf("年龄:");
scanf("%d",&stu[i].age);
printf("数学:");
scanf("%lf",&stu[i].math_score);
printf("语文:");
scanf("%lf",&stu[i].chinese_score);
printf("物理:");
scanf("%lf",&stu[i].physical_score);
printf("化学:");
scanf("%lf",&stu[i].chemical_score);
printf("生物:");
scanf("%lf",&stu[i].biological_score);
printf("英语:");
scanf("%lf",&stu[i].english_score);
i++;
}
break;
case 2:
FILE *f1 = fopen("./infom.txt","r+");
while(i<2)
{
int res = fprintf(f1,"%s%d%lf%lf%lf%lf%lf%lf",stu[i].name,
stu[i].age,stu[i].math_score,stu[i].english_score,
stu[i].chinese_score,stu[i].physical_score,stu[i].chemical_score,stu[i]
.biological_score);
i++;
}
fclose(f1);
break;
case 3:
FILE *f2 = fopen("./infom.txt","r+");
while(i<2)
{
int res1 = fscanf(f2,"%s%d%lf%lf%lf%lf%lf%lf",&stu[i].name,
&stu[i].age,&stu[i].math_score,&stu[i].english_score,
&stu[i].chinese_score,&stu[i].physical_score,&stu[i].chemical_score,&stu[i]
.biological_score);
i++;
}
fclose(f2);
for(i=0;i<2;i++)
{
printf("姓名:%s ",stu[i].name);
printf("年龄:%d\n",stu[i].age);
printf("数学:%lf ",stu[i].math_score);
printf("英语:%lf ",stu[i].english_score);
printf("语文:%lf ",stu[i].chinese_score);
printf("物理:%lf ",stu[i].physical_score);
printf("化学:%lf ",stu[i].chemical_score);
printf("生物:%f\n",stu[i].biological_score);
}
break;
case 4:
break;
case 5:
return 0;
}
}
return 0;
}