返回顶部

收藏

linux 下 more 命令 的c++实现

更多

linux 下 more 命令 的c++实现

#include <stdio.h>
#define PAGELEN 24
#define LINELEN 512
void do_more(FILE *);
int see_more();
int main(int ac,char **av[])
{
    FILE * fp;
    if(ac == 1 )
        do_more(stdin);
    else 
        while(--ac)
            if((fp = fopen(* ++av,"r")) != NULL)
            {
                do_more(fp);    
                fclose(fp); 
            }
            else 
                exit(1);
    return 0;

}
void do_more(FILE *fp)
{
    char line[LINELEN];
    int num_of_lines  = 0;
    int see_more(),reply;
    while(fgets(line,LINELEN,fp))
    {
        if(num_of_lines == PAGELEN)         
        {
            reply = see_more(); 
            if(reply == 0)
                break;
            num_of_lines -= reply;
        }
        if(fputs(line,stdout) == EOF)
            exit(1);
        num_of_lines++;

    }

}
int see_more()
{
    int c;
    printf("\033[7m more?\033[m");
    while((c = getchar()) != EOF)
    {
        if(c == 'q')    
            return 0;
        if(c == ' ')
            return PAGELEN;
        if(c == '\n')
            return 1;

    }
    return 0;
}

标签:cpp,more

收藏

0人收藏

支持

1

反对

0

相关聚客文章
  1. 屁屁 发表 2012-12-16 01:34:23 Ready For More
  2. Freewind 发表 2012-12-31 08:02:15 推荐衡天小张的博客主机
  3. Raman Sharma 发表 2015-04-29 21:00:00 More about resumable functions in C++
  4. 博主 发表 2014-09-05 00:00:00 CPP Info Memo Part 3
  5. 博主 发表 2015-05-19 14:49:33 多态性之运算符重载
  6. 博主 发表 2015-08-05 15:30:29 C++ 检查工具:valgrind 和 cppcheck
  7. 博主 发表 2015-09-22 18:09:34 One more Academic Writing Company Falls under Scru
  8. 博主 发表 2015-06-15 00:00:00 Optimizing&#32;CPP&#58;&#32;The&#32;Costs&#32;Of&#
  9. bigwhite 发表 2016-05-16 13:11:54 理解Unikernels
  10. sherry 发表 2016-11-02 09:11:35 如何让“less is more”的设计理论在设计作品中平衡
  11. hzlzh 发表 2015-01-21 11:58:01 What exactly is a dissertation? How is it more adv
  12. serendipity 发表 2013-03-15 09:43:29 ” As the afternoon wore on, and more dominoes were

发表评论