stand IO input & output

1. getchar( void ) , getc( stdin ), fgetc( stdin )

    getchar ( void )   = =  getc ( stdin )  

    getc( stdin )  --- >  #define Macro == fgetc( stdin ) 

    fgetc( stdin ) --- >  Function  = =  getc ( stdin )  


2.Whichever of the 3 above is used to get  only one char from the INPUT stream, 

   which have cache buffer by default. 

   so  after the first time you use the "getchar()" ,  

   the sencond time when you want to get one char,  you can use "double" getchar() to avoid the'\n'in buffer. 

   or  I tried to write this to reslove the problem . 

   

printf("getc(fp):");           // 1st time
	c_input = getc(stdin);

	printf("getchar:");
	while(( c_input_2 = getchar())==0x0a);   // 2nd .. other  time 

      Note of that the method " fflush ( stdin ) "  can only be used in VC, not gcc enviroment.  

                   Also the method " setbuf( stdin, NULL ) " can't work well in my test.

3. putchar( void ) , putc( int c,  stdout ), fputc( int c , stdout )

    the similar define as input ...


4. gets( buf )  &  fgets( buf ,  N,  stdin ) 

    fgets( buf,  size N ,   stdin  )   ------------   read one line from stdin buffer 

    para  N  read size:   N - 1  char ,   can read  '\n'  char .

    if read chars  <  (N-1) , there would be a '\n' read from the stdin buffer.

    gets( buf )  --------------   dangerous , for it doesn't point out the buf Size. 

    output_gets.c:(.text+0x54): warning: the `gets' function is dangerous and should not be used.

5. puts ( buf )  &  fputs( buf, stdout )  
------- puts ( ) -------
Hello puts() !
This is China
Over  ( auto '\n' )

------- fputs( buf,stdout ) -------
Hello  fputs() !
This is China
Over:  (add '\n' manual)

6. Binary IO stream   fwrite ( const void * ptr,  sizeof(Obj),  ObjNum,  File * fp ) 

                                      fread ( void * ptr,  sizeof(Obj),  ObjNum,  File *fp ) 

                                      f r e a d和f w r i t e返回读或写的对象数。


对于读,如果出错或到达文件尾端,则此数字可以少于n o b j 在这种情况,应调用f e r r o r或f e o f以判断究竟是那一种情况;

对于写,如果返回值少于所要求的n o b j,则出错。


7. FILE * fp__;       fp->_flag & _IONBF , 

    FILE 指针结构体, 'FILE' has no member named   为什么找不到 _flag 成员 ? _bufsiz 成员?

    暂时还没找到答案 ( ---- >  因为旧版本的结构体 与新版本的 结构体 成员名发生变化,

                                                  请使用fp->_IO_file_flags , _IO_UN_BUFFERED, _IO_LINE_BUF, 没有size,使用fp->_IO_buf_end - fp->_IO_buf_base 


8. Temp File  tmpnam() ,  tempnam()  ,  tmpfile() ,  mkstemp() 

    There is a drawback to using tmpnam and tempnam: a window exists between the time 

    that the unique pathname is returned and the time that an application creates a file with that name. 

    During this timing window, another process can create a file of the same name.

    1)  tmpnam( NULL )  --- >    Return a static char * pointer to   Tmp file Name, just name , file doesn't exist yet.

    2)  tmpnam( char * L_tmpnam )  --- >    Store into the char *  for Tmp file Name. 

         Step 1 :   using  1) or 2)  , then  using   tmpfile ()  to  make real  temp file in path called from above 2 functions. 

    3)  tempnam ( char * Dir ,   char * pre-fix )  --- >    using the  $TMPDIR env first than Dir 

     

   $ ./a.out /home/sar TEMP                specify both directory and prefix
   /home/sar/TEMPsf00zi
   $ ./a.out " " PFX                       use default directory: P_tmpdir
   /tmp/PFXfBw7Gi
   $ TMPDIR=/var/tmp ./a.out /usr/tmp " "  use environment variable; no prefix
   /var/tmp/file8fVYNi                     environment variable overrides directory
   $ TMPDIR=/no/such/dir ./a.out /home/sar/tmp QQQ
   /home/sar/tmp/QQQ98s8Ui                 invalid environment directory is ignored


   4)  int mkstemp( char *  tmpfile )  --- >  normally used functions 

        file created by mkstemp is not removed automatically for us,  Need  unlink()  it  ourselves 

int mkstemp(char *template);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值