#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;
}
运行结果