Linux下C语言编程基础知识(4)

2019-02-15 12:02

一个目录.telldir和seekdir类似与ftee和fseek函数.

下面我们开发一个小程序,这个程序有一个参数.如果这个参数是一个文件名,我们输出这 个文件的大小和最后修改的时间,如果是一个目录我们输出这个目录下所有文件的大小和 修改时间.

#i nclude #i nclude #i nclude #i nclude #i nclude #i nclude #i nclude

static int get_file_size_time(const char *filename) {

struct stat statbuf;

if(stat(filename,&statbuf)==-1) {

printf(\ filename,strerror(errno));

return(-1); }

if(S_ISDIR(statbuf.st_mode))return(1); if(S_ISREG(statbuf.st_mode))

printf(\

filename,statbuf.st_size,ctime(&statbuf.st_mtime)); return(0); }

int main(int argc,char **argv) {

DIR *dirp;

struct dirent *direntp; int stats; if(argc!=2) {

printf(\ exit(1); }

if(((stats=get_file_size_time(argv[1]))==0)||(stats==-1))exit(1); if((dirp=opendir(argv[1]))==NULL) {

printf(\ argv[1],strerror(errno));

exit(1);

}

while((direntp=readdir(dirp))!=NULL) if(get_file_size_time(direntp- | int pipe(int fildes[2]);

pipe调用可以创建一个管道(通信缓冲区).当调用成功时,我们可以访问文件描述符fild es[0],fildes[1].其中fildes[0]是用来读的文件描述符,而fildes[1]是用来写的文件描 述符. 在实际使用中我们是通过创建一个子进程,然后一个进程写,一个进程读来使用的 . 关于进程通信的详细情况请查看进程通信

#i nclude #i nclude #i nclude #i nclude #i nclude #i nclude #i nclude

#define BUFFER 255

int main(int argc,char **argv) {

char buffer[BUFFER+1]; int fd[2]; if(argc!=2) {

fprintf(stderr,\ exit(1); }

if(pipe(fd)!=0) {

fprintf(stderr,\ exit(1); }

if(fork()==0) {

close(fd[0]);

printf(\ snprintf(buffer,BUFFER,\ write(fd[1],buffer,strlen(buffer)); printf(\ exit(0); }

else

{

close(fd[1]);

printf(\ memset(buffer,'',BUFFER+1); read(fd[0],buffer,BUFFER);

printf(\ exit(1); } }

为了实现重定向操作,我们需要调用另外一个函数dup2. #i nclude

int dup2(int oldfd,int newfd);

dup2将用oldfd文件描述符来代替newfd文件描述符,同时关闭newfd文件描述符.也就是说

, 所有向newfd操作都转到oldfd上面.下面我们学习一个例子,这个例子将标准输出重定 向到一个文件.

#i nclude #i nclude #i nclude #i nclude #i nclude #i nclude #i nclude

#define BUFFER_SIZE 1024

int main(int argc,char **argv) {

int fd;

char buffer[BUFFER_SIZE]; if(argc!=2) {

fprintf(stderr,\ exit(1); }

if((fd=open(argv[1],O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR))==-1)

{

fprintf(stderr,\ exit(1);

}

if(dup2(fd,STDOUT_FILENO)==-1) {

fprintf(stderr,\ exit(1); }

fprintf(stderr,\ fprintf(stderr,\ while(1) {

fgets(buffer,BUFFER_SIZE,stdin); if(feof(stdin))break;

write(STDOUT_FILENO,buffer,strlen(buffer)); } exit(0); }

好了,文件一章我们就暂时先讨论到这里,学习好了文件的操作我们其实已经可以写出一 些比较有用的程序了.我们可以编写一个实现例如dir,mkdir,cp,mv等等常用的文件操作,命令了. 想不想自己写几个试一试呢?

Linux下的时间概念 (第四章)

作者:Hoyt Email:hoytluo@21cn.com 前言:

这一章我们学习Linux的时间表示和计算函数 时间的表示 时间的测量 计时器的使用

----------------------------------------------------------------------------

1。时间表示

在程序当中,我们经常要输出系统当前的时间,比如我经常使用date命

令来看看我已经工作了几个小时和当前的时间.其实我们很容易实现date这个命令的.这 个时候我们可以使用下面两个函数

#i nclude

time_t time(time_t *tloc); char *ctime(const time_t *clock);

time函数返回从1970年1月1日0点以来的秒数.存储在time_t结构之中.不过这个函数的返

回值对于我们来说没有什么实际意义.这个时候我们使用第二个函数将秒数转化为字符串

. 这个函数的返回类型是固定的:一个可能值为. Thu Dec 7 14:58:59 2000 这个字符串 的长度是固定的为26

/* 一个非常简单的date实现 */ #i nclude int main() {

time_t now; time(&now);

printf(\ return(0); }

注意ctime的返回字符串最后2个字符是\\n.所以我们没有必要进行最后输出的换行符号.

2。时间的测量

有时候我们要计算程序执行的时间.比如我们要对算法进行时间分析.这个时候可以 使用下面这个函数.

#i nclude

int gettimeofday(struct timeval *tv,struct timezone *tz); strut timeval {

long tv_sec; /* 秒数 */ long tv_usec; /* 微秒数 */ };

gettimeofday将时间保存在结构tv之中.tz一般我们使用NULL来代替. #i nclude #i nclude #i nclude

void function() {

unsigned int i,j; double y; for(i=0;i

int getitimer(int which,struct itimerval *value); int setitimer(int which,struct itimerval *newval, struct itimerval *oldval); struct itimerval {

struct timeval it_interval;


Linux下C语言编程基础知识(4).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:耐纳特橡胶板性能分类

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: