2.29文件IO-进程 作业

1.要求将当前路径下,所有文件的权限及最后一次的访问时间提取出来,写入到file.txt中! !提示: opendir readir stat-->提取出来的数据写入到file.xt中

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


int main(int argc, const char *argv[])
{
	DIR* op = opendir("../01标准IO");
	if( op == NULL )
	{
		perror("opendir");
		return -1;
	}
	printf("文件打开成功!!!\n");
	
	FILE* fop = fopen("./test2_29hk.txt","w");

	struct dirent* rd = NULL;  
	struct stat msg;
	while(1)
	{

		rd = readdir(op);
		if(rd == NULL )
		{
			if( 0 == errno)
				printf("文件以读完!!\n");
			else
				perror("readdir\n");
			break;
		}

		if( stat(rd->d_name,&msg) < 0 )
		{
			perror("stat\n");
			return -1;
		}
		printf("%o--->",msg.st_mode);
		struct tm* date;
		date = localtime(&msg.st_ctime);
		printf("%04d-%02d-%02d %02d:%02d:%02d\n",\
				date->tm_year+1900, date->tm_mon+1, date->tm_mday,\
				date->tm_hour, date->tm_min, date->tm_sec);

		fprintf(fop,"%o  %04d-%02d-%02d %02d:%02d:%02d\n",msg.st_mode,\
				date->tm_year+1900, date->tm_mon+1, date->tm_mday,\
				date->tm_hour, date->tm_min, date->tm_sec);

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

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

使用文件IO对图片进行拷贝。要求子进程拷贝后半部分,父进程拷贝前半部分。提示:

1.图片就是一个普通文件,普通文件怎么拷贝,图片就怎么拷贝。读一个字符,写一个字符,循环size/2次

2.diff可以查看两个文件是否一致,例如diff 1.png 2.png,可以查看两张图片是否一致

3.终端输入: eog 1.png可以打开1.png这张图片

#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("./cq.jpg",&msg) < 0)
	{
		perror("stat");
		return -1;
	}
	printf("%ld\n",msg.st_size);
	int size = msg.st_size;

	int cq = open("./cq.jpg",O_RDONLY);
	int op1 = open("./f.jpg",O_WRONLY|O_CREAT|O_TRUNC,0775);
//	int op2 = open("./z.jpg",O_WRONLY|O_CREAT|O_TRUNC,0775);

	pid_t cpid = fork();

	if( cpid > 0)
	{
		//父
		char c;
		int i=0;
		while( read(cq, &c, sizeof(c)) > 0 && i<(size/2) )
		{	
			write(op1, &c,sizeof(c));
			i++;
		}
		off_t d1 = lseek(cq, 0, SEEK_CUR);
		printf("第一次移动到大小%ld\n",d1);
	}
	else if( cpid == 0 )
	{
		// 子
		sleep(1);
		char d;
        int j=0;
		off_t d2 = lseek(cq, -1, SEEK_CUR);
        while( read(cq, &d, sizeof(d)) > 0)
        {
            write(op1, &d,sizeof(d));
        }
	}
	else
	{
		perror("fork");
		return -1;
	}

	while(1)
	{
		sleep(1);
	}

	close(op1);

	return 0;
}

 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值