stdin, stdout, stderror

来源:互联网 发布:怎么做一名淘宝客 编辑:程序博客网 时间:2024/05/08 01:10

cat x.c y.c

文件指针它指向一个包含文件信息的结构,这些信息包括:缓冲区的位置、缓冲区中当前字符的位置、文件的读或写状态、是否出错或是否已经到达文件结尾等等.
fp 是一个指向结构 FILE 的指针,并且,fopen 函数返回一个指向结构 FILE 的指针。

参考:http://bbs.csdn.net/topics/391821551

/* <stdio.h> include a structure declaration called FILE. (with a typedef) */FILE *fp;/* read ("r"), write ("w"), and append ("a") ,r+w+a("a+")*/FILE *fopen (char *name, char *mode);int fclose(FILE *fp);/* getc returns the next character from the stream referredto by fp; it returns EOF for end of file or error. */int getc(FILE *fp);/* putc writes the character c to the file fp and return the character written, or EOF if an error occurs. */int putc(int c, FILE *fp);
#include <stdio.h>int main (){  int c = getc(stdin); //i  putc(c, stdout);    //i  FILE *fp;  fp = fopen("test.c", "a+");  c = getc(fp);       //#  putc(c, stdout);    //#  putc(c, fp);        //in the end of '}'#     fclose(fp);   return 0;} 

getchar 和 putchar,getc 和 putc 是而不是函数。

When a C program is started, the operating system environment is responsible
for opening three files and providing pointers for them.
the corresponding file pointers are called stdin, stdout, and stderr,
and are declared in

#define getchar() getc(stdin)#define putchar(c) putc((c), stdout)

对于文件的格式化输入或输出,可以使用函数 fscanffprintf.

int fscanf(FILE *fp, char *format, ...);int fprintf(FILE *fp, char *format, ...);

stdin 与 stdout 都是 FILE *类型的对象。但它们是常量.

#include <stdio.h>/*FILE *fopen(char *name, char *mode);int getc (FILE *fp);int putc (int c, FILE *fp);int fscanf(FILE *fp, char *format, ...);int fprintf(FILE *fp, char *format, ...);*//* cat : concatenate files, version 1 */int main (int argc, char *argv[]){        FILE *fp;        void filecopy(FILE *, FILE *);        if (argc == 1)        /* no args; copy standard input */                filecopy(stdin, stdout);        while (--argc > 0)                if ((fp = fopen(*++argv, "r")) == NULL){                        printf("cat: can't open %s\n", *argv);                        return 1;                } else {                        filecopy(fp, stdout);                        fclose(fp);                }        return 0;}/* filecopy: copy file ifp to ofp */void filecopy (FILE *ifp, FILE *ofp){        int c;        while ((c = getc(ifp)) != EOF)                putc(c, ofp);}

错误处理———stderr & exit

exit 为每个已打开的输出文件调用 fclose 函数,以将缓冲区中的所有输出写到相应的文件中.
在主程序 main 中,语句 return expr 等价于 exit(expr).

#include <stdio.h>/* cat: concatenate files, version 2 */int main (int argc, char *argv[]){    FILE *fp;    void filecopy(FILE *, FILE *);    char *prog = argv[0];   /* program name for errors */       if (argc == 1)  /* no args; copy standard input */        filecopy(stdin, stdout);    else        while (--argc > 0) {            if ((fp = fopen(*++argv, "r")) == NULL){                fprintf(stderr, "%s: can't open %s\n", prog, *argv);                exit(1);                }            else {                filecopy(fp, stdout);                fclose(fp);            }        }    if (ferror(stdout)) {        fprintf(stderr, "%s: error writing stdout\n", prog);        exit(2);    }    exit(0);}void filecopy(FILE *ifp, FILE *ofp){    int c;    while((c = getc(ifp)) != EOF)        putc(c, ofp);}           
//如果流 fp 中出现错误,则函数`ferror`返回一个非 0 值.int ferror(FILE *fp);

行输入和行输出

/* fgets reads the next input line(including the newline) from file fp into the character array line; at most maxline-1 character will be read. on end of file or error it returns NULL. */char *fgets(char *line, int maxline, FILE *fp);/* `fputs` writes a string (which need not contain a newline) to a file. It returns EOF if an error occurs, and non-negative otherwise. */int fputs(char *line, FILE *fp);

库函数getsputs的功能与fgetsfputs函数类似, 但它们是对stdinstdout 进行操作。
有一点我们需要注意,gets 函数在读取字符串时将删除结尾的换行符(‘\n’)
puts 函数在写入字符串时将在结尾添加一个换行符。

ANSI 标准规定,ferror 在发生错误时返回非 0 值,而 fputs 在发生错误时返回 EOF,
其它情况返回一个非负值。

#include <stdio.h>/* fgets: get at most n chars from iop */char *fgets (char *s, int n, FILE *iop){        register int c;        register char *cs;        cs = s;        while(--n > 0 && (c = getc(iop)) != EOF)                if ((*cs++ = c) == '\n')                        break;        *cs = '\0';        return (c == EOF && cs == s) ? NULL : s;}/* fputs: put string s on the file iop */int fputs (char *s, FILE *iop){        int c;        while (c = *s++)                putc(c, iop);        return ferror(iop) ? EOF : 0;}/* getline: read a line, return length */int getline (char *line, int max){        if (fgets(s, max, stdin) == NULL)                return 0;        else                return strlen(line);}
0 0
原创粉丝点击