The exercises in the third chapter of the fourth edition of Linux programming

Source: Internet
Author: User

1, P103

A directory scanner.


 #include <stdio.h> #include <dirent.h> #include <sys/stat.h>int    Isadir (char* path); Determine if the path is a directory type void Printdirs (char* path,int depth)//recursively traverse the print file with the directory name {dir* dir=opendir (path); struct dirent* Dirents;chdir ( Path), while (Dirents=readdir (dir)) {if (Isadir (dirents->d_name)) {if (strcmp (".", Dirents->d_name) ==0| | strcmp ("..", Dirents->d_name) ==0) continue;printf ("%*s%s/\ n", depth, "", Dirents->d_name);p Rintdirs (dirents- >d_name,depth+1);} elseprintf ("%*s%s/\ n", depth, "", Dirents->d_name);} ChDir (".."); Closedir (dir);} int Isadir (char* path) {struct stat statbuff;//printf ("%s", path); Lstat (Path,&statbuff); if (S_isdir (statbuff.st_ mode) {return 1;} return 0;}          int main (int argc,char* argv[]) {/* char* path[1023];gets (path);     Pathchdir (Path) entered by the user in the program, */char* path= "."; Use the program's parameters when Pathif (argc >=2) {path = argv[1];} if (Isadir (path) ==1) {printdirs (path,0);p error ("Wrong::"); return 1;} printf ("Path is not a dir"); return 1;} 


2, P112

Use the MMAP function program to modify the contents of the file, map the file to memory, and change it as an array!

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/ Mman.h> #define RECORDNUM (a) struct       record//recording structure {int integ;char string[15];} Record;int creatfile (char* file) {struct Record R; file* fp=fopen (file, "w+"); int i=0;for (; i<50;i++) {r.integ=i;memset (r.string,0,15);      sprintf (r.string, "record-%d", I);   format string input into R.string fwrite (&r,sizeof (R), 1,FP);} Fclose (FP);} void Memomap (char* file) {struct record record,*r;int f=open (FILE,O_RDWR); r= (struct record *) mmap (0, Recordnum*sizeof ( struct Record), prot_read| Prot_write, map_shared, f,0);   Memory-mapped functions r[20].integ=100;sprintf (r[20].string, "record-%d", R[20].integ), Msync ((void*) r,recordnum*sizeof (Record), Ms_sync);               Save the modification of the memory segment back to the file Munmap ((void*) r,recordnum*sizeof (RECORD));//Free memory segment Close (f); return;} int main (int argc,char* argv[]) {creatfile ("records.date"); Memomap ("records.date"); return 1;}



The exercises in the third chapter of the fourth edition of Linux programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.