【摘要】 介紹Linux下目錄編程、標(biāo)準(zhǔn)文件編程、Linux系統(tǒng)文件接口編程、GDB調(diào)試等知識點(diǎn)。
任務(wù)1:目錄操作
學(xué)習(xí)系統(tǒng)編程: 主要學(xué)習(xí)系統(tǒng)函數(shù)的調(diào)用。
#include /* int argc :表示傳入的參數(shù)數(shù)量 char **argv:存放傳入的參數(shù)數(shù)據(jù),本身是一個(gè)二維數(shù)組。 argv[0] 表示第一個(gè)參數(shù) argv[1] 表示第二個(gè)參數(shù) */ int main(int argc,char **argv) { int i; for(i=0;i;i++)<> { printf("argv[%d]=%s\n",i,argv[i]); } return 0; } |
文件操作函數(shù): fopen、fclose、fread、fwrite…………
目錄操作函數(shù):
#include #include DIR *opendir(const char *name); //打開目錄 struct dirent *readdir(DIR *dirp); //讀取目錄信息 int closedir(DIR *dirp); //關(guān)閉目錄 |
struct dirent { ino_t d_ino; /* inode number */ off_t d_off; /* offset to the next dirent */ unsigned short d_reclen; /* length of this record */ unsigned char d_type; /* type of file; not supported by all file system types */ char d_name[256]; /* filename 文件名稱 */ }; |
?? (練習(xí)目錄操作)
1.?實(shí)現(xiàn)ls *.c 類似的功能。 產(chǎn)品: 視頻播放器、音樂播放器
2.?強(qiáng)化第一題。ls *.c
(1)?拼接路徑: /mnt/hgfs/linux-share-dir/test/test.c (通過命令行傳入路徑)
示例:./a.out ./work/test .c
結(jié)果: ./work/test/123.c ./work/test/456.c
(2)?獲取絕對路徑: /mnt/hgfs/linux-share-dir/test/test.c
示例:./a.out ./work/test .c
結(jié)果: /user/work/test/123.c /user/work/test/456.c
3.?實(shí)現(xiàn)目錄的拷貝。 類似cp命令
int mkdir(const char *pathname, mode_t mode); |
(1)?實(shí)現(xiàn)一層目錄的拷貝。
(2)?實(shí)現(xiàn)所有目錄的拷貝。(擴(kuò)展)
char *strstr(const char *haystack, const char *needle); const char *haystack:在哪里查找 const char *needle:查找的數(shù)據(jù) 返回值: 返回查找到的數(shù)據(jù)的首地址 |
4.?實(shí)現(xiàn)cat命令效果
(1)?示例: cat 123.c
(2)?示例: cat 123.c -n (擴(kuò)展作業(yè))
5.?實(shí)現(xiàn)du命令的功能。
(1)?示例: du 123.c 顯示出文件的大小
(2)?示例: du 123.c -h 使用合理的單位顯示出文件的大小(擴(kuò)展作業(yè))
1.1 獲取當(dāng)前目錄絕對地址相關(guān)函數(shù)
#include char *getcwd(char *buf, size_t size); char *getwd(char *buf); char *get_current_dir_name(void); //獲取絕對路徑 int chdir(const char *path); //修改當(dāng)前目錄,即切換目錄,相當(dāng)于 cd 命令 |
1.2 mkdir函數(shù)創(chuàng)建目錄
#include #include int mkdir(const char *pathname, mode_t mode); // 777 #include #include mode_t umask(mode_t mask); //設(shè)置創(chuàng)建目錄或者文件的默認(rèn)權(quán)限 一般在調(diào)用mkdir時(shí),需要先調(diào)用umask(0); mode_t mode參數(shù)的填寫說明: S_IRWXU 00700 user (file owner) has read, write and exe-cute permission S_IRUSR 00400 user has read permission S_IWUSR 00200 user has write permission S_IXUSR 00100 user has execute permission S_IRWXG 00070 group has read, write and execute permis-sion S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 others have read, write and execute per-mission S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission |
1.3 獲取目錄和文件的狀態(tài)信息
#include #include #include int stat(const char *path, struct stat *buf); //直接獲取源文件的狀態(tài)() int fstat(int fd, struct stat *buf); //獲取已經(jīng)打開成功的文件狀態(tài) int lstat(const char *path, struct stat *buf); //不區(qū)分鏈接文件 //存放文件的狀態(tài)信息 struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* 保存文件的大小*/ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ }; m的參數(shù):-->st_mode S_ISREG(m) 判斷是否是普通文件 S_ISDIR(m) directory? 目錄 S_ISCHR(m) character device? S_ISBLK(m) block device? S_ISFIFO(m) FIFO (named pipe)? S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.) S_ISSOCK(m) socket? (Not in POSIX.1-1996.) |
任務(wù)2:帶緩沖區(qū)的文件IO操作
文件中的3個(gè)標(biāo)準(zhǔn)指針:
stderr Standard error output stream. 存放標(biāo)準(zhǔn)錯(cuò)誤信息的文件 stdin Standard input stream. 存放標(biāo)準(zhǔn)輸入信息的文件 stdout Standard output stream. 存放標(biāo)準(zhǔn)輸出信息的文件 |
??示例:
#include int main() { char buff[10]; while(1) { if(fread(buff,1,10,stdin)>0) { printf("讀出的數(shù)據(jù)=%s\n",buff); } } return 0; } |
帶緩沖區(qū):這一系列函數(shù)適合操作普通文件。
size_t fread(void *restrict, size_t, size_t, FILE *restrict); int fseek(FILE *, long, int); size_t fwrite(const void *restrict, size_t, size_t, FILE *restrict); int fclose(FILE *); |
任務(wù)3:非緩沖區(qū)的文件IO操作
open()
#include #include #include int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); //在創(chuàng)建文件的時(shí)候才調(diào)用。 ssize_t read(int fd, void *buf, size_t count); ssize_t write(int fd, const void *buf, size_t count); int close(int fd); |
??作業(yè)
1.?練習(xí)read、write讀寫函數(shù)。
2.?將cat命令編寫使用open一系列函數(shù)實(shí)現(xiàn)。
3.?將stat文件狀態(tài)的時(shí)間轉(zhuǎn)為標(biāo)準(zhǔn)時(shí)間打印出來。
st_atime=1516528093 st_mtime=1516586761 st_ctime=1516586761 以上3個(gè)值是秒單位的時(shí)間。 是從1970年1月1日0時(shí)0分0秒開始計(jì)算。 |
格式: xxxx年xx月xx日 xx時(shí):xx分:xx秒 星期x
轉(zhuǎn)換時(shí)間:(寫出一個(gè)類似于時(shí)鐘的程序,可以加一個(gè)鬧鐘,判斷時(shí)間是否到達(dá))
(1)?編寫一個(gè)函數(shù)實(shí)現(xiàn)秒單位的時(shí)間轉(zhuǎn)為標(biāo)準(zhǔn)時(shí)間
(2)?編寫一個(gè)函數(shù)實(shí)現(xiàn)將標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)為秒單位
#include int main(int argc,char**argv) { while(1) { long cnt=time(NULL); //獲取當(dāng)前系統(tǒng)時(shí)間 sleep(1); //睡眠一秒鐘 printf("cnt=%d\n",cnt); } return 0; } |
4.?擴(kuò)展作業(yè):重寫一個(gè)scanf與printf函數(shù)。
int printf(const char *format, ...); int scanf(const char *format, ...); |
…可變形參
任務(wù)4:安裝編輯器
??壓縮與打包名稱
.1.1 解壓命令的使用格式: tar -xvf <壓縮文件的名稱>
1.1.?想要指定解壓之后文件存放的位置: tar -xvf <壓縮文件的名稱> -C <指定的目錄>
1.2 打包指定文件夾或者文件: tar -cvf <新文件的名稱> <要打包的目錄>
示例: [root@wbyq test]# tar cvf SublimeText2.tar SublimeText2
注意:壓縮包不能直接在共享目錄下解壓。
??運(yùn)行軟件:
(1)?# ./sublime_text
(2)?# ./sublime_text & (后臺運(yùn)行程序)
??切換中文輸入法:Ctrl+空格
??linux操作系統(tǒng)保存可執(zhí)行文件的環(huán)境變量: PATH
(1)?打印環(huán)境變量的值:echo $PATH
(2)修改環(huán)境變量:# vim /etc/profile (系統(tǒng)開機(jī)的時(shí)候自動(dòng)執(zhí)行)
(3) source命令立即生效環(huán)境變量。(只是針對當(dāng)前的終端)
示例:# source /etc/profile
任務(wù)5: GDB調(diào)試器
使用GDB調(diào)試工具: gdb <可執(zhí)行文件的名稱>
示例: gdb a.out
要支持調(diào)試程序,需要在gcc編譯的時(shí)候加上-g選項(xiàng)參數(shù)。
示例:gcc -g 123.c
??常用的命令:
breakpoints -- 設(shè)置程序的斷點(diǎn)(簡寫b)。 語法: b <行號>或者 b <函數(shù)名稱> running -- 開始運(yùn)行程序(簡寫run) list -- 打印出當(dāng)前的代碼 next -- 單步調(diào)試 quit -- 退出調(diào)試界面 |
-
Linux
+關(guān)注
關(guān)注
87文章
11351瀏覽量
210494 -
編程
+關(guān)注
關(guān)注
88文章
3639瀏覽量
94028 -
GDB調(diào)試
+關(guān)注
關(guān)注
0文章
24瀏覽量
1490
發(fā)布評論請先 登錄
相關(guān)推薦
評論