#include <stdio.h>
#include <syspes.h>
#include <sysat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sysat.h>
int main(int argc, const char *argv[])
{
int fp = open("./1.png", O_RDONLY);
if(fp < 0)
{
perror("open1");
return -1;
}
int fo = open("./2.png", O_WRONLY | O_CREAT | O_TRUNC);
if(fo < 0)
{
perror("open2");
return -1;
}
struct stat buf;
int s = stat("./1.png", &buf);
if(s < 0)
{
perror("stat");
return -1;
}
// printf("size = %ld\n",buf.st_size);
int size = buf.st_size / 2;
printf("k = %d\n",k);
char str = 0;
int len = 0;
pid_t pid = fork();
if(pid > 0)
{
sleep(2);
lseek(fp, 0, SEEK_SET);
lseek(fo, 0, SEEK_SET);
while( len < szie)
{
read(fp, &str, 1);
write(fo, &str, 1);
len++;
}
printf("前面拷贝完成\n");
}
else if(pid == 0)
{
len = size;
lseek(fp, k , SEEK_SET);
lseek(fo, k , SEEK_SET);
while(len < buf.st_size)
{
read(fp, &str, 1);
write(fo, &str, 1);
len++;
}
printf("后面拷贝完成\n");
}
else
{
perror("fork");
return -1;
}
close(fp);
close(fo);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, const char *argv[])
{
DIR* dr = opendir("../611_dir/");
if(NULL == dr)
{
perror("opendir");
return 0;
}
FILE* fp = fopen("./4.text", "w");
if(NULL == fp)
{
perror("fopen");
return 0;
}
struct dirent * rdir = NULL;
struct stat buf;
while(1)
{
rdir = readdir(dr);
if(NULL == rdir)
break;
if(rdir->d_name[0] != '.')
continue;
stat("./1.c", &buf);
fprintf(fp, "%d", buf.st_mode);
fprintf(fp, "%ld", buf.st_atime);
printf("\n");
}
fclose(fp);
closedir(dr);
return 0;
}