《Beginning Linux Programming》读书笔记(三)

简介:
1,文件的读写

0号文件描述符—标准输入,1号文件描述符—标准输出,2号文件描述符—标准错误

复制代码
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
    int srcFile,desFile;
    char *srcPath = "/home/phinecos/hello.c";//source file
    char *desPath = "/home/phinecos/hello_back.c";//dest file
    srcFile = open(srcPath,O_RDONLY);//open source file
    if(srcFile==-1)
    {//open source file failed
        write(2,"open err\n",9);
        return -1;
    }
    desFile = open(desPath,O_CREAT|O_WRONLY,S_IRUSR|S_IWUSR);//open dest file
    if(desFile==-1)
    {//open dest file failed
        write(2,"open err\n",9);
        return -1;
    }
    char buffer[256];
    int nReaded;
    while((nReaded=read(srcFile,buffer,256))>0)
    {//read source into buffer,then write buffer  into dest
        write(desFile,buffer,nReaded);
    }
    close(srcFile);//close source file
    close(desFile);//close dest file
    return 0;
}
复制代码
2,lstat和stat的区别是当文件是一个符合链接时,lstat返回链接本身的信息,而stat返回的是链接所指向的文件的信息。

3,遍历一个目录

复制代码
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

void printdir(char *dir, int depth)
{
    DIR *dp;
    struct dirent *entry;
    struct stat statbuf;
    if((dp = opendir(dir)) == NULL) {
        fprintf(stderr,"cannot open directory: %s\n", dir);
        return;
    }
    chdir(dir);
    while((entry = readdir(dp)) != NULL) {
        lstat(entry->d_name,&statbuf);
        if(S_ISDIR(statbuf.st_mode)) {
            /* Found a directory, but ignore . and .. */
            if(strcmp(".",entry->d_name) == 0 || 
                strcmp("..",entry->d_name) == 0)
                continue;
            printf("%*s%s/\n",depth,"",entry->d_name);
            /* Recurse at a new indent level */
            printdir(entry->d_name,depth+4);
        }
        else printf("%*s%s\n",depth,"",entry->d_name);
    }
    chdir("..");
    closedir(dp);
}
/*  Now we move onto the main function.  */
int main()
{
    printf("Directory scan of /home:\n");
    printdir("/home",0);
    printf("done.\n");

    exit(0);
}
复制代码
4,内存映射文

复制代码
#include <unistd.h>
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>

//记录结构体
typedef struct 
{
    int integer;//标号
    char string[24];//名称
} RECORD;

#define NRECORDS (100)

int main()
{
    RECORD record, *mapped;
    int i, f;
    FILE *fp;
    fp = fopen("records.dat","w+");
    for(i=0; i<NRECORDS; i++) 
    {
        record.integer = i;
        sprintf(record.string,"RECORD-%d",i);
        fwrite(&record,sizeof(record),1,fp);
    }
    fclose(fp);
/*  We now change the integer value of record 43 to 143
    and write this to the 43rd record's string.  */
    fp = fopen("records.dat","r+");
    fseek(fp,43*sizeof(record),SEEK_SET);
    fread(&record,sizeof(record),1,fp);
    record.integer = 143;
    sprintf(record.string,"RECORD-%d",record.integer);
    fseek(fp,43*sizeof(record),SEEK_SET);
    fwrite(&record,sizeof(record),1,fp);
    fclose(fp);
/*  We now map the records into memory
    and access the 43rd record in order to change the integer to 243
    (and update the record string), again using memory mapping.  */
    f = open("records.dat",O_RDWR);
    mapped = (RECORD *)mmap(0, NRECORDS*sizeof(record), PROT_READ|PROT_WRITE, MAP_SHARED, f, 0);
    mapped[43].integer = 243;
    sprintf(mapped[43].string,"RECORD-%d",mapped[43].integer);
    msync((void *)mapped, NRECORDS*sizeof(record), MS_ASYNC);
    munmap((void *)mapped, NRECORDS*sizeof(record));
    close(f);
    exit(0);
}

复制代码


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/11/24/1340071.html,如需转载请自行联系原作者
目录
相关文章
|
5月前
|
缓存 IDE Linux
《Linux是怎么样工作的》读书笔记(二)
《Linux是怎么样工作的》读书笔记
65 0
《Linux是怎么样工作的》读书笔记(二)
|
5月前
|
存储 缓存 固态存储
《Linux是怎么样工作的》读书笔记(一)
《Linux是怎么样工作的》读书笔记
123 0
《Linux是怎么样工作的》读书笔记(一)
|
消息中间件 存储 网络协议
《硬核linux攻略》读书笔记更新中
《硬核linux攻略》读书笔记更新中
|
Shell Linux Perl
《Linux Shell脚本攻略》读书笔记
《Linux Shell脚本攻略》读书笔记
212 0
|
存储 Shell Linux
《Linux命令行与shell脚本编程大全》读书笔记————第一章 初识Linux shell
本章内容 1、什么是Linux 2、Linux内核的组成   1、1 什么是Linux Linux课划分为以下四部分 a)Linux内核 b)GNU工具 c)图形化桌面环境 d)应用软件   1.1.1 深入探究Linux内核 内核主要负责以下四种功能 a)系统内存管理 b)软件程序管理 c)硬件设备管理 d)文件系统管理   1、系统内存管理 内核不仅管理服务器上的可用内存,还可以创建和管理虚拟内存(即实际上不存在的内存)。
1279 0
|
关系型数据库 Linux
《Linux就该这么学》读书笔记
cat /proc/cpuinfo lsmod 安装VNC服务 重置root密码: image.png RPM操作: image.png 日期: image.
1284 0
《Linux内核设计与实现》读书笔记 - 目录 (完结)
《Linux内核设计与实现》读书笔记 - 目录 (完结) 读完这本书回过头才发现, 第一篇笔记居然是 2012年8月发的, 将近一年半的时间才看完这本书(汗!!!).
|
12月前
|
存储 安全 编译器
[笔记]读书笔记 C++设计新思维《一》基于策略的类设计(下)
[笔记]读书笔记 C++设计新思维《一》基于策略的类设计(下)
|
12月前
|
存储 关系型数据库 编译器
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
C++ Primer Plus 第6版 读书笔记(9)第 9章 函数——内存模型和名称空间
102 1
|
12月前
|
存储 算法 编译器
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
C++ Primer Plus 第6版 读书笔记(8)第 8章 函数探幽(二)
62 1
下一篇
无影云桌面