清明假期作业

文章介绍了如何用C语言实现文件夹拷贝、使用`structstudent`结构体数组进行文件I/O操作以及创建并通信的父子进程示例。涉及函数如`my_copy`、`f_write`、`f_read`等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值