IO进程线程第三天(7.31)time,localtime,文件io函数:open,umask,close,write,read,lseek,stat加XMind

用read函数完成图片文件拷贝

#include
#include
int main(int argc, const char *argv[])
{
    //umask(0);//将文件权限掩码改为0,使得其他用户可写
    int fd = open("/home/ubuntu/图片/2.jpg",O_RDONLY,0777);//打开图片
    if(fd<0)
    {   
        ERR_MSG("open");
        return -1; 
    }   
    printf("打开成功\n");
    int ff = open("1.jpg",O_WRONLY|O_CREAT|O_TRUNC,0777);//创建图片
    if(ff<0)
    {   
        ERR_MSG("open");
        return -1; 
    }   
    printf("打开成功\n");
    char a[20]="";
    ssize_t res;
    while(1)
    {   
        res=read(fd,a,sizeof(a)-1);
        if(0==res)
            break;
        if(write(ff,a,res)<0)
        {
            ERR_MSG("write");
            return -1; 
        }
    }   
    printf("拷贝完成\n");
    if(close(fd)<0)
    {   
        ERR_MSG("close");
        return -1;                                                                                  
    }   
    printf("关闭成功\n");
    if(close(ff)<0)
    {   
        ERR_MSG("close");
        return -1; 
    }   
    printf("关闭成功\n");

    return 0;
} 

用read实现计算文件大小

#include
#include
int main(int argc, const char *argv[])
{
    int ff = open("1.jpg",O_RDONLY);
    if(ff<0)
    {   
        ERR_MSG("open");
        return -1; 
    }   
    printf("打开成功\n");
    off_t size=lseek(ff,0,SEEK_END);
    printf("图片大小为:%ld\n",size);
    char a;
    long count=0;                                                                                                                
    lseek(ff,0,SEEK_SET);
    while(read(ff,&a,1)>0)
    {   
        count++;
    }   
    printf("图片大小为:%ld\n",count);
    if(close(ff)<0)
    {   
        ERR_MSG("close");
        return -1; 
    }   
    printf("关闭成功\n");

    return 0;
} 

IO进程线程第三天(7.31)time,localtime,文件io函数:open,umask,close,write,read,lseek,stat加XMind_第1张图片

 将文件权限提取修改成循环方式

#include
#include
void get_filePermission(mode_t m)
{
    int a[9]={0400,0200,0100,0040,0020,0010,0004,0002,0001};
    char b[9]={'r','w','x','r','w','x','r','w','x'};
    for(int i=0;i<9;i++)
    {   
        if((m&a[i])==0)
            putchar('-');
        else
            putchar(b[i]);
    }   
}
int main(int argc, const char *argv[])
{
    struct stat buf;
    if(stat("./time.c",&buf)<0)
    {   
        ERR_MSG("stat");
        return -1; 
    }   
    printf("mode: 0%o\n",buf.st_mode);
    get_filePermission(buf.st_mode);
    printf("\n");
    printf("link: %ld\n",buf.st_nlink);
    printf("uid: %d\n",buf.st_uid);
    printf("gid: %d\n",buf.st_gid);
    printf("size: %ld\n",buf.st_size);
    printf("time: %ld\n",buf.st_ctime);

    return 0;
}

IO进程线程第三天(7.31)time,localtime,文件io函数:open,umask,close,write,read,lseek,stat加XMind_第2张图片

IO进程线程第三天(7.31)time,localtime,文件io函数:open,umask,close,write,read,lseek,stat加XMind_第3张图片

 

你可能感兴趣的:(算法,开发语言,c语言,IO,XMind)