- 论坛徽章:
- 84
|
本帖最后由 yjh777 于 2016-02-25 15:45 编辑
好吧,有点标题党的味道
原来的写法:- void dtype2str(unsigned int dtype, char *buf)
- {
- switch(dtype) {
- case DT_BLK: strcpy(buf, "DT_BLK");
- break;
- case DT_CHR: strcpy(buf, "DT_CHR");
- break;
- case DT_DIR: strcpy(buf, "DT_DIR");
- break;
- case DT_FIFO: strcpy(buf, "DT_FIFO");
- break;
- case DT_LNK: strcpy(buf, "DT_LNK");
- break;
- case DT_REG: strcpy(buf, "DT_REG");
- break;
- case DT_SOCK: strcpy(buf, "DT_SOCK");
- break;
- case DT_UNKNOWN: strcpy(buf, "DT_UNKNOWN");
- break;
- default: strcpy(buf, "undefine");
- break;
- }
- }
- ..snip..
- dtype2str(d_type, buf);
- printf("%s\n", buf);
复制代码 改为三目运算符的写法:
..snip..- printf("%-10s ", (d_type == DT_REG) ? "regular" :
- (d_type == DT_DIR) ? "directory" :
- (d_type == DT_FIFO) ? "FIFO" :
- (d_type == DT_SOCK) ? "socket" :
- (d_type == DT_LNK) ? "symlink" :
- (d_type == DT_BLK) ? "block dev" :
- (d_type == DT_CHR) ? "char dev" : "???");
复制代码 |
|