图片拷贝
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
int p_r=open("./1.png",O_RDONLY);
int p_w=open("./2.png",O_WRONLY|O_CREAT|O_TRUNC,0777);
off_t size=lseek(p_r,0,SEEK_END);
pid_t pid=fork();
if(pid>0)
{
sleep(3);
lseek(p_r,0,SEEK_SET);
lseek(p_w,0,SEEK_SET);
char c;
for(int i=0;i<size/2;i++)
{
read(p_r,&c,1);
write(p_w,&c,1);
}
printf("父线程拷贝完毕\n");
}
if(pid==0)
{
lseek(p_r,size/2,SEEK_SET);
lseek(p_w,size/2,SEEK_SET);
char arr[30];
ssize_t res;
while(res!=0)
{
res=read(p_r,arr,29);
write(p_w,arr,res);
}
printf(".............\n");
}
close(p_r);
close(p_w);
return 0;
}
信息查询
#include "stdio.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE* pb=fopen("./txt","w");
if(pb==NULL)
{
perror("fopen");
return -1;
}
printf("文件打开成功\n");
DIR *dir=opendir("../100t/");
if(NULL==dir)
{
perror("opendir");
return -1;
}
printf("文件夹打开成功\n");
struct dirent *dp;
char name[256]="";
struct stat buf;
struct tm* info;
while(1)
{
dp=readdir(dir);
if(dp==NULL)
{
if(errno==0)
{
printf("w文件读取成功\n");
}
else
{
perror("readdir");
return -1;
}
}
if(dp->d_name[0]=='.')
{
continue;
}
fprintf(pb,"%s\t",dp->d_name);
stat(dp->d_name,&buf);
info=localtime(&buf.st_ctim);
fprintf(pb,"%o %04d-%02d-%02d-%02d-%02d %s\n",buf.st_mode & 0777,\
info->tm_year+1900,info->tm_mon+1,info->tm_mday,\
info->tm_hour,info->tm_min,dp->d_name);
}
closedir(dir);
fclose(pb);
return 0;
}