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

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值