本文主要是介绍清明假期作业,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、实现文件夹的拷贝功能
注意判断被拷贝的文件夹是否存在,如果不存在则提前
不考虑递归拷贝的问题
#include<myhead.h>
int my_copy(char* name,const char *p)
{char buf[256]="./";strcat(buf,p);strcat(buf,"/");strcat(buf,name);int rfd=open(name,O_RDONLY);int wfd=open(buf,O_WRONLY|O_CREAT,0664);char arr[126];while(1){int res=read(rfd,arr,sizeof(arr));if(res<0){perror("read");return 1;}else if(res==0){break;}write(wfd,arr,sizeof(arr));bzero(arr,sizeof(arr));}close(rfd);close(wfd);
}
int main(int argc, char *argv[])
{DIR* fd=opendir(argv[1]);if(fd==NULL){perror("opendir");return 1;}mkdir(argv[2],0777);while(1){struct dirent* rd=readdir(fd);if(rd==NULL&&errno==0){break;}else if(rd==NULL&&errno!=0){perror("readdir");return -1;}if(*(rd->d_name)=='.'){continue;}my_copy(rd->d_name,argv[2]);}closedir(fd);return 0;
}
效果图:
2、有如下结构体
struct studentf
char name[20];
int age;
double math score;
double chinese score;
double english score;
double physical score;
double chemical score;
double biological score;
申请一个该结构体数组,使用 fprintf /fscanf,将该结构体数组中的所有数据,写入文件中
#include<myhead.h>
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;
};
int main(int argc, char *argv[])
{struct Student stu[5];int i=0;for(i=0;i<5;i++){scanf("%s %d %lf %lf %lf %lf %lf %lf",stu[i].name,&stu[i].age,&stu[i].math_score,&stu[i].chinese_score,&stu[i].english_score,&stu[i].physical_score,&stu[i].chemical_score,&stu[i].biological_score);while(getchar()!=10);}for(i=0;i<5;i++){printf("姓名:%s\n",stu[i].name);printf("年龄:%d\n",stu[i].age);printf("数学:%lf\n",stu[i].math_score);printf("语文:%lf\n",stu[i].chinese_score);printf("英语:%lf\n",stu[i].english_score);printf("物理:%lf\n",stu[i].physical_score);printf("化学:%lf\n",stu[i].chemical_score);printf("生物:%lf\n",stu[i].biological_score);}FILE* wfd=fopen("./write.txt","w");for(i=0;i<5;i++){fprintf(wfd,"%s %d %lf %lf %lf %lf %lf %lf",stu[i].name,stu[i].age,stu[i].math_score,stu[i].chinese_score,stu[i].english_score,stu[i].physical_score,stu[i].chemical_score,stu[i].biological_score);}fclose(wfd);FILE* rfd=fopen("./write.txt","r");struct Student new[5];for(i=0;i<5;i++){fscanf(rfd,"%s %d %lf %lf %lf %lf %lf %lf",new[i].name,&new[i].age,&new[i].math_score,&new[i].chinese_score,&new[i].english_score,&new[i].physical_score,&new[i].chemical_score,&new[i].biological_score);}for(i=0;i<5;i++){printf("姓名:%s\n",new[i].name);printf("年龄:%d\n",new[i].age);printf("数学:%lf\n",new[i].math_score);printf("语文:%lf\n",new[i].chinese_score);printf("英语:%lf\n",new[i].english_score);printf("物理:%lf\n",new[i].physical_score);printf("化学:%lf\n",new[i].chemical_score);printf("生物:%lf\n",new[i].biological_score);}fclose(rfd);return 0;
}
创建一对父进程,在父进程能够向子进程发送消息的基础上发同时子进程也能够向父进程发送消息
#include<myhead.h>
char* f_write(char *buf)
{int wfd=open("./f_write.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);write(wfd,buf,sizeof(buf));close(wfd);return buf;
}
char* f_read(char *buf)
{int rfd=open("./f_write.txt",O_RDONLY);read(rfd,buf,sizeof(buf));close(rfd);return buf;
}
char* z_write(char *buf)
{int wfd=open("./z_write.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);write(wfd,buf,sizeof(buf));close(wfd);return buf;
}
char* z_read(char *buf)
{int rfd=open("./z_write.txt",O_RDONLY);read(rfd,buf,sizeof(buf));close(rfd);return buf;
}
int main(int argc, char *argv[])
{int res=fork();while(1){if(res>0){printf("请输入父进程信息\n");char wbuf[1024];scanf("%s",wbuf);f_write(wbuf);while(1){struct stat rstat;stat("z_write.txt",&rstat);if(rstat.st_size>0){break;}}char rbuf[1024]={0};z_read(rbuf);printf("子进程:%s\n",rbuf);}else if(res==0){while(1){struct stat rstat;stat("f_write.txt",&rstat);if(rstat.st_size>0){break;}}char rbuf[1024]={0};f_read(rbuf);printf("父进程:%s\n",rbuf);printf("请输入回复\n");char wbuf[1024];scanf("%s",wbuf);z_write(wbuf);}else if(res==-1){perror("fork");return 1;}}return 0;
}
这篇关于清明假期作业的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!