Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119036
  • 博文数量: 29
  • 博客积分: 1215
  • 博客等级: 中尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-05 16:29
文章分类
文章存档

2010年(29)

我的朋友

分类: C/C++

2010-12-05 21:48:55

预备知识:
1.NAME
       fgetc,  fgets,  getc,  getchar,  gets, ungetc - input of characters and
       strings

SYNOPSIS
       #include

       int fgetc(FILE *stream);

       char *fgets(char *s, int size, FILE *stream);

       int getc(FILE *stream);

       int getchar(void);

       char *gets(char *s);

       int ungetc(int c, FILE *stream);
DESCRIPTION
       fgetc() reads the next character from  stream  and  returns  it  as  an
       unsigned char cast to an int, or EOF on end of file or error.

       getc()  is equivalent to fgetc() except that it may be implemented as a
       macro which evaluates stream more than once.

       getchar() is equivalent to getc(stdin).

       gets() reads a line from stdin into the buffer pointed to  by  s  until
       either  a  terminating newline or EOF, which it replaces with '\0'.  No
       check for buffer overrun is performed (see BUGS below).

       fgets() reads in at most one less than size characters from stream  and
       stores  them  into  the buffer pointed to by s.  Reading stops after an
       EOF or a newline.  If a newline is read, it is stored into the  buffer.
       A '\0' is stored after the last character in the buffer.

       ungetc()  pushes  c  back to stream, cast to unsigned char, where it is
       available for subsequent read operations.  Pushed-back characters  will
       be returned in reverse order; only one pushback is guaranteed.
RETURN VALUE
       fgetc(),  getc() and getchar() return the character read as an unsigned
       char cast to an int or EOF on end of file or error.

       gets() and fgets() return s on success, and NULL on error or  when  end
       of file occurs while no characters have been read.

       ungetc() returns c on success, or EOF on error.


2.NAME
       fputc, fputs, putc, putchar, puts - output of characters and strings

SYNOPSIS
       #include

       int fputc(int c, FILE *stream);

       int fputs(const char *s, FILE *stream);

       int putc(int c, FILE *stream);

       int putchar(int c);

       int puts(const char *s);
DESCRIPTION
       fputc() writes the character c, cast to an unsigned char, to stream.

       fputs() writes the string s to stream, without its trailing '\0'.

       putc()  is equivalent to fputc() except that it may be implemented as a
       macro which evaluates stream more than once.

       putchar(c); is equivalent to putc(c,stdout).

       puts() writes the string s and a trailing newline to stdout.
RETURN VALUE
       fputc(), putc() and  putchar()  return  the  character  written  as  an
       unsigned char cast to an int or EOF on error.

       puts()  and  fputs()  return a nonnegative number on success, or EOF on
       error.
3.NAME
       clearerr, feof, ferror, fileno - check and reset stream status

SYNOPSIS
       #include

       void clearerr(FILE *stream);

       int feof(FILE *stream);

       int ferror(FILE *stream);

       int fileno(FILE *stream);
DESCRIPTION
       The function clearerr() clears the end-of-file and error indicators for
       the stream pointed to by stream.

       The function feof() tests the  end-of-file  indicator  for  the  stream
       pointed  to by stream, returning nonzero if it is set.  The end-of-file
       indicator can only be cleared by the function clearerr().

       The function ferror() tests the error indicator for the stream  pointed
       to  by stream, returning nonzero if it is set.  The error indicator can
       only be reset by the clearerr() function.

       The function fileno() examines the  argument  stream  and  returns  its
       integer descriptor.

本程序用标准IO将标准输入复制到标准输出(能复制任一UNIX普通文件)。
程序清单:

#include <stdio.h> //printf(),getc(),putc(),ferror(),EOF

#include <stdlib.h> //exit(0),exit(1);


int main(int argc,char * argv[])
{
    char c;
    while((c = getc(stdin))!=EOF)
    {
        if(putc(c,stdout) == EOF)
        {
            printf("output error!\n");
            exit(1);
        }
        if(ferror(stdin))
        {
            printf("input error\n");
            exit(1);
        }
    }
    exit(0);
}

编译源文件:
  gcc -o stdinout stdinout.c
执行程序:
./stdinout
Hello
Hello
How old are you?
How old are you?
你怎么老是复制我呀?
你怎么老是复制我呀?
若以以下方式执行该程序:
./stdinout < infile > outfile
那么名为infile文件的内容复制到名为outfile的文件中。


阅读(1430) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-12-07 10:15:30

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com