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

xiaoxiao2022-06-12  29

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

相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4933120.html

最新回复(0)