Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)
来源:互联网 发布:js二维数组姓名,年龄 编辑:程序博客网 时间:2024/06/06 11:03
Linux下的IO/文件操作练习,知识虽然简单 但是往往基础容易被忽略,偶尔的练习是有必要的。
练习printf
/*************************************************************************> File Name: printf.c> Author: > Mail: > Created Time: Wed 11 Feb 2015 01:08:15 AM PST ************************************************************************/#include<stdio.h>int main(){ printf("%10s\n","hello"); printf("%-10s\n","hello"); printf("%s\n","hello") ; printf("%*s\n",10,"hello") ; printf("%010d\n",100) ; printf("%10.4g\n",1.22) ; printf("%10s\n","Hello,I am a programmer !") ; return 0 ;}
练习C标准库的文件复制 相比系统调用 会快 因为有缓冲
/*************************************************************************> File Name: cpy.c> Author: > Mail: > Created Time: Wed 11 Feb 2015 01:37:56 AM PST ************************************************************************/#include<stdio.h>int main(){ int c; FILE*in=fopen("./printf","r"); FILE*out=fopen("./printf_tem","w"); while((c=fgetc(in))!=EOF) { fputc(c,out) ; } exit(0);}
练习查看一个文件的状态
/*************************************************************************> File Name: fileinfo.c> Author: > Mail: > Created Time: Wed 11 Feb 2015 11:25:21 PM PST ************************************************************************/#include<stdio.h>#include<sys/stat.h>#include<unistd.h> int main(){ int fileNo; struct stat fileStat ; FILE*pFile= fopen("./cpy","r"); if(pFile==NULL) { printf("File Open Error!\n") ; exit(0); } fileNo= fileno(pFile); printf("FileNumber:%d\n",fileNo) ; if(-1==fstat(fileNo,&fileStat)) { printf("GetFileInfo Error!\n") ; exit(0) ; } printf("DeviceID:%d\n",fileStat.st_dev); printf("UserID:%d\n",fileStat.st_uid); printf("GroupID:%d\n",fileStat.st_gid); printf("FileSize:%d\n",fileStat.st_size); return 0 ;}
我们模拟Linux下的ls程序
/*************************************************************************> File Name: listdir.c> Author: > Mail: > Created Time: Thu 12 Feb 2015 12:49:13 AM PST ************************************************************************/#include<stdio.h>#include<unistd.h>#include<sys/stat.h>#include<dirent.h>#include<string.h>#include<sys/types.h>void list_func(char*path,int depth) { DIR*pDirHandle= opendir(path); struct dirent * dent ; struct stat fstat ; if(pDirHandle==NULL) { printf("OpenDir %s Error!\n",path); exit(0); } chdir(path); while((dent=readdir(pDirHandle))!=NULL) { //error then return -1 lstat(dent->d_name,&fstat); if(S_ISDIR(fstat.st_mode)) { //remove director . and .. if(strcmp(".",dent->d_name)==0|| strcmp("..",dent->d_name)==0 ) continue ; printf("%*s%s/\n",depth,"",dent->d_name) ; list_func(dent->d_name,depth+4) ; }else printf("%*s%s\n",depth,"",dent->d_name); } chdir(".."); closedir(pDirHandle);}int main(int argc,char**argv){ if(argc<2) { printf("Param Format: listdir path\n"); return ; } char*pDirPath=argv[1]; int depath=0; printf("List Begin:\n"); list_func(pDirPath,depath) ; printf("List End.\n"); return 0 ;}
格式化错误代码
/*************************************************************************> File Name: errformat.c> Author: > Mail: > Created Time: Fri 13 Feb 2015 12:50:00 AM PST ************************************************************************/#include<stdio.h>#include<errno.h>#include<stdlib.h>#include<string.h>int main(){ char* message=NULL ; //error fopen("aaaaa","r") ; // printf(strerror(errno)); message=strerror(errno); if(message!=NULL) printf("Error:%s\n",message); perror("Program:"); return 0;}
1 1
- Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)
- Linux显示文件树形结构信息的小程序
- Linux C编写的列出文件树形结构的程序
- Linux C程序设计系列之 练习系统调用文件IO、内存映射程序 编写ls程序
- linux c模拟ls命令
- linux下ls命令模拟
- Linux C 文件IO操作
- Linux下C编程入门笔记——文件IO操作(一)
- Linux下C编程入门笔记——文件IO操作(二)
- Linux下C编程入门笔记——文件IO操作
- Linux下C编程-----IO/文件操作/内存映射 实现简单记录存储(3)
- linux c编程 文件IO
- Linux ls显示文件分类
- linux tree命令以树形结构显示文件目录结构
- linux--tree命令以树形结构显示文件目录结构
- linux 如何以树形结构显示文件目录结构
- Linux下的C编程--文件系统编程
- linux下C编程之文件系统编程
- Unicode 转换成 Ascii (把Unicode 中文字符串输入到文本中)
- 获得平行四边形中,那条较长的中心线
- Zookeeper的回调函数
- SQL优化--物理优化--表扫描与连接算法
- App接口开发经验谈(2)
- Linux下C编程-----IO/文件操作 模拟linux ls程序显示文件系统树形结构(2)
- JAVAC 命令详解
- HDU 2501 Tiling_easy version
- iOS学习笔记(4)——浅谈KVC与KVO
- ftp
- sqlite3 编译问题
- chipscope配置
- (四十九)Quartz2D自定义控件
- UVA 11488 Hyper Prefix Sets Trie树求最值