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