二、Input and output-2013-4-9

本文深入探讨了C语言中的标准输入输出、格式化输出、变量长度参数列表、格式化输入、文件访问及错误处理等内容,以及如何正确使用fgets、gets、puts等函数进行文本读写操作。
Input and output are not part of the C language itself。
 
一、Standard Input and Output
a、The symbolic constant EOF is defined in <stdio.h>. The value is typically -1, bus  tests should be written in terms of EOF so as to
      be independent of the specific  value.

b、"functions" like getchar and putchar in <stdio.h> and tolower in <ctype.h> are often macros, thus avoiding the overhead of a function call per character.

 
二、Formatted Output - printf
a、printf converts, formats, and prints its arguments on the standard output , It returns the number of characters printed
b、printf uses its first argument to decide how many arguments follow and what their type is.

 
三、Variable-length Argument Lists
a、The standard header <stdarg.h> contains a set of macro definitions that define how to step through an argument list.
b、The implementation of this header will vary from machine to machine, but the  interface it presents is uniform.
c、structure and operate
/* typedef struct {
          char *a0;       /* pointer to first homed integer argument */
         int offset;     /* byte offset of next parameter */
 } va_list;
*/
 typedef char *va_list;
 #define _INTSIZEOF(n)   ( (sizeof(n) + sizeof(int) - 1) & ~(sizeof(int) - 1) )
 #define va_start(ap,v)  ( ap = (va_list)&v + _INTSIZEOF(v) )
 #define va_arg(ap,t)    ( *(t *)((ap += _INTSIZEOF(t)) - _INTSIZEOF(t)) )
 #define va_end(ap)      ( ap = (va_list)0 )


四、Formatted Input - Scanf
a、scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results  
       through the remaining arguments.

b、the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored.

c、There is also a function sscanf that reads from a string instead of the standard  input:
       int sscanf(char *string, char *format, arg1, arg2, ...)

d、The function ferror returns non-zero if an error occurred on the stream fp.
 
五、File Access
a、This pointer, called the file pointer, points to a structure that contains information about the file

b、The call to fopen in a program is
       fp = fopen(name, mode);
      The second argument is the mode, also a character string, which indicates how  one intends to use the file. Allowable modes   
   include read ("r"), write ("w"),  and append ("a").  If there is any error, fopen will return NULL.

c、the operating system environment is responsible for opening three files and providing pointers for them. These files are the
     standard input, the standard output, and the standard error; the corresponding file pointers are called   stdin, stdout, and stderr

d、Normally stdin is connected to the keyboard and stdout and stderr are connected to the screen, but stdin and stdout may be 
     redirected to files or pipes as described

e、fclose is the inverse of fopen, it breaks the connection between the file pointer and the external name that was established   by
     fopen, freeing the file pointer for  another file.

f、fclose is called automatically for each open file when a program terminates normally.
 
六、Error Handling - Stderr and Exit
a、Output written on stderr normally appears on the screen even if the standard output is redirected.
b、The argument of exit is available to whatever process called this one
c、exit calls fclose for each open output file, to flush out any buffered output.
d、Within main, return expr is equivalent to exit(expr). exit has the advantage that  it can be  called from other functions

 
七、Line Input and Output
a、fgets reads the next input line (including the newline) from file fp into the  character array line; at most maxline-1 characters will be
      read. The resulting line is terminated with '\0'.

b、Confusingly, gets deletes the terminating '\n', and puts adds it.

 
八、Storage Management
 The right way is to save whatever is needed before freeing: (list)
 for (p = head; p != NULL; p = q)
 {
  q = p->next;
  free(p);
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值