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