IO:实现拷贝,以及提取数据,写到另一个文件。

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h> 
#include <sys/stat.h>
#include <fcntl.h>



int main(int argc, const char *argv[])
{	int id;
	id=open("./1.png",O_RDONLY);

	int cpid=open("cpy.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
	if(cpid<0)
	{
		perror("open");
		return -1;
	}

	off_t size=lseek(id,0,SEEK_END);

	pid_t pid;
	pid=fork();

	if(pid>0)
	{
		sleep(4);
		lseek(id,0,SEEK_SET);
		lseek(cpid,0,SEEK_SET);
		char a;
		for(int i=0;i<size/2;i++)
		{
			read(id,&a,1);
			write(cpid,&a,1);
		}
		printf("前拷贝完毕\n");
	}

	else if(pid==0)
	{
		lseek(id,size/2,SEEK_SET);
		lseek(cpid,size/2,SEEK_SET);


		char  b;
		for(int i=0;i<size/2+1;i++)
		{
			read(id,&b,1);
			write(cpid,&b,1);
		}

	printf("后拷贝完成\n");
	}
	else{
		perror("fork");
		return -1;
	}

	close(id);
	close(cpid);
	return 0;
}

运行结果

#include <stdio.h>
#include <dirent.h>
#include <dirent.h>
#include <time.h>
#include <string.h>
#include <errno.h> 
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>


int main(int argc, const char *argv[])
{
	FILE *fp=fopen("./file.txt","w");
	DIR *pr;
	pr=opendir(argv[1]);
	if(pr==NULL)
	{
		perror("opendir");
		return -1;
	}
	printf("文件打开成功\n");
	
	struct stat buf;
	struct dirent* ap;
	char str[300]="";
	struct tm *ptr;
	while(1)
	{
	ap=readdir(pr);
	if(ap==NULL)
	{
		if(errno==0)
		{
			printf("文件读取完毕\n");
		}else
		{
			perror("readdir");
			return -1;
		}
	}
	if(ap->d_name[0]=='.')
	{
	 	continue;
	}
	printf("name=%s\n",ap->d_name);

	strcpy(str,argv[1]);
	strcat(str,ap->d_name);


	if(stat(str,&buf)<0)
	{
		perror("stat");
		return -1;
	}

	ptr=localtime(&buf.st_ctime);
	fprintf(fp,"%o %d-02%d-02%d 02%d:02%d:02%d %s\n",buf.st_mode & 0777,ptr->tm_year+1900,\
			ptr->tm_mon+1,ptr->tm_mday,ptr->tm_hour,ptr->tm_min,ptr->tm_sec,ap->d_name);
	}

	if(closedir(pr)<0)
	{
		perror("closedir");
		return -1;
	}
	fclose(fp);
	return 0;
}

运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值