2024/2/29

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

int main(int argc, const char *argv[])
{
	DIR* op = opendir("../4_IO进线程/");
	if(op == NULL)
	{
		perror("opendir");
		return -1;
	}
		FILE* opp = fopen("./file1.txt","w");
	struct dirent*rd = NULL;
	struct stat xinxi;
	while(1)
	{
		rd = readdir(op);
		if(rd == NULL)
		{
			if( 0 == errno )
				printf("读取完成\n");
			else
				perror("reddir");
			break;
		}
		if(stat(rd->d_name,&xinxi)<0)
		{
			printf("stat");
			return -1;
		}
		struct tm* riqi;
		riqi = localtime( &xinxi.st_atime );
	
		fprintf(opp, "%o %04d-%02d-%02d %02d:%02d:%02d\n", xinxi.st_mode,\
				riqi->tm_year+1900,riqi->tm_mon+1,riqi->tm_mday,\
				riqi->tm_hour,riqi->tm_min,riqi->tm_sec);	
	}
	if(fclose(opp)<0)
	{

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

		perror("closedir");
		return -1;
	}


	return 0;
}

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

int main(int argc, const char *argv[])
{
	struct stat msg;
	if(stat("./jj.jpg",&msg)<0)
	{
		perror("stat");
		return -1;
	}
	printf("%ld\n",msg.st_size);
	int size = msg.st_size;
	int tp  = open("./jj.jpg",O_RDONLY);
	int tp1 = open("./jj1,jpg",O_WRONLY|O_CREAT|O_TRUNC,0775);

	pid_t cpid =  fork();

	if(cpid>0)
	{
		char a;
		int i=0 ;
		for(i = 0;read(tp,&a,sizeof(a))>0 && i<(size/2);i++)
		{
			write(tp1,&a,sizeof(a));
		}
		off_t yidong1 = lseek(tp , 0 ,SEEK_CUR);
		printf("第一次移动:%ld\n", yidong1);
	}
	else if(cpid == 0 )
	{
		sleep(1);
		char b;
		int j = 0;
		for(j=0;read(tp, &b,sizeof(b))>0;j++)
		{
			write(tp1,&b,sizeof(b));
		}
	}
	else
	{
		perror("fork");
		return -1;
	}

	close(tp1);

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值