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

1,文件的读写

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

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #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 " ; // sourcefile
char * desPath = " /home/phinecos/hello_back.c " ; // destfile
srcFile = open(srcPath,O_RDONLY); // opensourcefile
if (srcFile ==- 1 )
{
// opensourcefilefailed
write( 2 , " openerr/n " , 9 );
return - 1 ;
}
desFile
= open(desPath,O_CREAT | O_WRONLY,S_IRUSR | S_IWUSR); // opendestfile
if (desFile ==- 1 )
{
// opendestfilefailed
write( 2 , " openerr/n " , 9 );
return - 1 ;
}
char buffer[ 256 ];
int nReaded;
while ((nReaded = read(srcFile,buffer, 256 )) > 0 )
{
// readsourceintobuffer,thenwritebufferintodest
write(desFile,buffer,nReaded);
}
close(srcFile);
// closesourcefile
close(desFile); // closedestfile
return 0 ;
}

2lstatstat的区别是当文件是一个符合链接时,lstat返回链接本身的信息,而stat返回的是链接所指向的文件的信息。

3,遍历一个目录

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #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 statstatbuf;
if ((dp = opendir(dir)) == NULL){
fprintf(stderr,
" cannotopendirectory:%s/n " ,dir);
return ;
}
chdir(dir);
while ((entry = readdir(dp)) != NULL){
lstat(entry
-> d_name, & statbuf);
if (S_ISDIR(statbuf.st_mode)){
/* Foundadirectory,butignore.and.. */
if (strcmp( " . " ,entry -> d_name) == 0 ||
strcmp(
" .. " ,entry -> d_name) == 0 )
continue ;
printf(
" %*s%s//n " ,depth, "" ,entry -> d_name);
/* Recurseatanewindentlevel */
printdir(entry
-> d_name,depth + 4 );
}
else printf( " %*s%s/n " ,depth, "" ,entry -> d_name);
}
chdir(
" .. " );
closedir(dp);
}
/* Nowwemoveontothemainfunction. */
int main()
{
printf(
" Directoryscanof/home:/n " );
printdir(
" /home " , 0 );
printf(
" done./n " );

exit(
0 );
}

4,内存映射文

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> #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()
{
RECORDrecord,
* 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);
/* Wenowchangetheintegervalueofrecord43to143
andwritethistothe43rdrecord'sstring.
*/
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);
/* Wenowmaptherecordsintomemory
andaccessthe43rdrecordinordertochangetheintegerto243
(andupdatetherecordstring),againusingmemorymapping.
*/
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 );
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值