输出权限
ubuntu@ubuntu:D4$ ./p1
mode= 100644
rw-r--r--
ubuntu@ubuntu:D4$ cat p1.c
#include "head.h"
#include <time.h>
void get_file(mode_t m);
void get_file(mode_t m)
{
int i;
int k1=256;
for(i=0;i<9;i++)
{
if(k1==2 || k1==16 || k1==128)
{
if((m & k1) != 0)
putchar('w');
else
putchar('-');
}
if(k1==4 || k1==32 || k1==256)
{
if((m & k1) != 0)
printf("r");
else
putchar('-');
}
if(k1==1 || k1==8 || k1==64)
{
if((m & k1) != 0)
putchar('x');
else
putchar('-');
}
k1 = k1>>1;
}
printf("\n");
}
int main()
{
struct stat buf;
if(stat("/home/ubuntu/hqyj/HQYJ/IO/D4/k1.c",&buf) < 0)
{
perror("error\n");
return -1;
}
printf("mode= %o\n",buf.st_mode);
get_file(buf.st_mode);
return 0;
}
read计算字节数
open success
co = 520
ubuntu@ubuntu:D4$ cat k3.c
#include "head.h"
int main()
{
umask(0);
char sread[128] = "";
int k1 = open("k3.c",O_RDONLY);
int t1 = open("p3.c",O_WRONLY | O_CREAT | O_TRUNC,0777);
if(k1 < 0 || t1 < 0)
{
perror("error\n");
return 0;
}
printf("open success\n");
int co = 0;
while(1)
{
bzero(sread,sizeof(sread));
int k2 = read(k1,sread,sizeof(sread)-1);
// co = lseek(k2,0,SEEK_CUR);
co = co + k2;
if(0 == k2)
{
break;
}
int k3 = write(t1,sread,k2);
}
printf("co = %d\n",co);
close(k1);
close(t1);
return 0;
}