黑馬《linux基礎程式設計》學習筆記(85到87)

2021-09-04 21:42:00 字數 2172 閱讀 9223

八十五. 讀取指定目錄下的普通檔案的個數——**

getfilenummer.c

#include #include #include #include #include int get_file_num(char* root)

// 判斷是不是目錄

if(ptr->d_type == dt_dir)

// 如果是普通檔案

if(ptr->d_type == dt_reg)

}closedir(dir);

return total;

}int main(int argc, char* argv)

int total = get_file_num(argv[1]);

printf("%s has regfile number: %d\n", argv[1], total);

return 0;

}

執行一下:

[root@vm_0_15_centos dir_op]# ls

chdir.c mkdir.c opendir.c readfilenum.c

[root@vm_0_15_centos dir_op]# gcc readfilenum.c

[root@vm_0_15_centos dir_op]# ./a.out

./a.out path[root@vm_0_15_centos dir_op]# ./a.out ./

./ has regfile number: 5

[root@vm_0_15_centos dir_op]# ./a.out /home

/home has regfile number: 6206

八十六. dup和dup2函式

八十七. dup和dup2測試

接下來看dup和dup2的例子

dup.c

#include #include #include #include #include #include #include int main(void)

// 複製檔案描述符

// int fd2 = dup(fd);

int fd2 = fcntl(fd, f_dupfd);

// 寫檔案

char* p = "讓程式設計改變世界。。。。。。";

write(fd2, p, strlen(p));

close(fd2);

char buf[1024];

lseek(fd, 0, seek_set);

read(fd, buf, sizeof(buf));

printf(" buf = %s\n", buf);

close(fd);

return 0;

}

dup2.c

#include #include #include #include #include #include #include int main(void)

if(fd2 == -1)

// 複製檔案描述符

dup2(fd, fd2);

// 寫檔案

char* p = "change the world by programing。。。。。。";

write(fd2, p, strlen(p));

close(fd2);

char buf[1024];

lseek(fd, 0, seek_set);

read(fd, buf, sizeof(buf));

printf(" buf = %s\n", buf);

close(fd);

return 0;

}

黑馬 linux學習筆記(一)

1.shell本質就是用名字來呼叫程式 2.常用快捷按鍵 ctrl p向後 ctrl n向前 ctrl a游標到行首 ctrl e游標到行位 ctrl h刪除游標前 ctrl d刪除游標後 游標本身 ctrl u刪除游標前 tab鍵 按一次補全,按兩次提示所有可能命令3.linux目錄結構 bin ...

黑馬 LINUX學習筆記(二)

1.vim的三種工作模式 1.命令模式 開啟檔案後,預設進入命令模式 2.編輯模式 需要輸入一些命令如iaosiaos等,切換到編輯模式 3.末行模式 在末行模式下輸入一些命令 2.圖示 3.常見的操作 1.游標的移動操作h j k l代表前下上後 2.0是行頭部 行尾部 3.gg移動游標到文字頭 ...

黑馬 LINUX學習筆記(三)

1.test.c為原始檔,e為啟用預處理檔案,test.i將處理後的檔案寫入test.i中 gcc e test.c o test.i2.生成彙編 到 test.s中 gcc s test.i o test.s3.將彙編 編譯成目標檔案 gcc c test.s o test.o 6.最簡單生成可執...