LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1001|回复: 3

基础性问题请教,初学Linux下C编程

[复制链接]
发表于 2006-8-29 22:19:14 | 显示全部楼层 |阅读模式
教材里面的一个自己编写的more程序,好多地方有些疑问,希望大家给出完整详细的解答,因为自学,所以代码看不懂非常难办。谢谢。

  1. /* more01.c  - version 0.1 of more
  2. *        read and print 24 lines then pause for a few special commands
  3. */

  4. #include        <stdio.h>

  5. #define        PAGELEN        24
  6. #define        LINELEN        512

  7. void do_more(FILE *);
  8. int  see_more();

  9. int main( int ac , char *av[] )
  10. {[color=red]①我以前教材中只见过是这样写的main(int argc,char *argv[])是不是上面的简写比较常用[/color]
  11.         FILE        *fp;

  12.         if ( ac == 1 )
  13.                 do_more( stdin );
  14.         else
  15.                 while ( --ac )
  16.                         if ( (fp = fopen( *++av , "r" )) != NULL )
  17.                         {
  18.                                 do_more( fp ) ;
  19.                                 fclose( fp );
  20.                         }
  21.                         else
  22.                                 exit(1);
  23.         return 0;
  24. }

  25. void do_more( FILE *fp )
  26. /*
  27. *  read PAGELEN lines, then call see_more() for further instructions
  28. */
  29. {
  30.         char        line[LINELEN];
  31.         int        num_of_lines = 0;
  32.         int        see_more(), reply;

  33.         while ( fgets( line, LINELEN, fp ) ){                /* more input        */
  34.                 if ( num_of_lines == PAGELEN ) {        /* full screen?        */
  35.                         reply = see_more();                /* y: ask user  */
  36.                         if ( reply == 0 )                /*    n: done   */
  37.                                 break;
  38.                         num_of_lines -= reply;                /* reset count        */
  39.                 }
  40.                 if ( fputs( line, stdout )  == EOF )        /* show line        */
  41.                         exit(1);                        /* or die        */
  42.                 num_of_lines++;                                /* count it        */
  43.         }
  44. }

  45. int see_more()
  46. /*
  47. *        print message, wait for response, return # of lines to advance
  48. *        q means no, space means yes, CR means one line
  49. */
  50. {
  51.         int        c;

  52.         printf("\033[7m more? \033[m");                /* reverse on a vt100        */
  53.         [color=red]②上面的颜色搭配具体是什么语法,任何实现这种前景和背景颜色.[/color]
  54.         while( (c=getchar()) != EOF )                        /* get response        */
  55.         {
  56.                 if ( c == 'q' )                        /* q -> N                */
  57.                         return 0;
  58.                 if ( c == ' ' )                        /* ' ' => next page        */
  59.                         return PAGELEN;                /* how many to show        */
  60.                 if ( c == '\n' )                /* Enter key => 1 line        */
  61.                         return 1;               
  62.         }
  63.         return 0;
  64. }

复制代码

谢谢帮我解答,这是more程序的第一个版本,后面还有一个版本。
发表于 2006-8-30 01:08:11 | 显示全部楼层
函数参数的名字只是用于函数内部引用,只要内部和函数定义保持一致就可以了。main函数的参数,只要类型符合定义,随便写成什么,只要是合法的标识符,都是可以的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-8-30 06:51:53 | 显示全部楼层
明白了,谢谢!
回复 支持 反对

使用道具 举报

发表于 2006-9-1 15:52:29 | 显示全部楼层
see_more函数的return也太多了一点吧?

个人建议还是先赋值再统一return比较好,因为以前听说“函数的出口太多不利于维护”,呵呵
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表