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

来源:互联网 发布:java 获取graphics 编辑:程序博客网 时间:2024/05/16 16:07

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

2lstatstat的区别是当文件是一个符合链接时,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);
}

 

原创粉丝点击