美文网首页
标准I/O库

标准I/O库

作者: yuq329 | 来源:发表于2020-06-29 15:50 被阅读0次

    标准I/O库

    #include <apue.h>
    #include <error.h>
    
    int main(void){
        int c;
        while((c=getc(stdin))!=EOF)
            if(putc(c,stdout) == EOF){
                err_sys("output error");
            }
    
        if(ferror(stdin)){
            err_sys("input error");
        }
    
        exit(0);
    }
    
    #include <apue.h>
    #include <error.h>
    
    int main(void) {
        char buf[MAXLINE];
    
        while (fgets(buf, MAXLINE, stdin) != NULL) {
            if (fputs(buf, stdout) == EOF) {
                err_sys("output error");
            }
        }
    
        if (ferror(stdin)) {
            err_sys("input error");
        }
    
        exit(0);
    }
    
    #include <apue.h>
    #include <error.h>
    
    void pr_stdio(const char *, FILE *);
    
    int is_unbuffered(FILE *);
    
    int is_linebuffered(FILE *);
    
    int buffer_size(FILE *);
    
    int main(void) {
        FILE *fp;
        fputs("enter any character\n", stdout);
        if (getchar() == EOF) {
            err_sys("getchar error");
        }
        fputs("one line to standard error\n", stderr);
        pr_stdio("stdin", stdin);
        pr_stdio("stdout", stdout);
        pr_stdio("stderr", stderr);
    
        if ((fp = fopen("/etc/passwd", "r")) == NULL) {
            err_sys("fopen error");
        }
        if (getc(fp) == EOF) {
            err_sys("getc error");
        }
        pr_stdio("/etc/passwd", fp);
        exit(0);
    }
    
    void pr_stdio(const char *name, FILE *fp) {
        printf("stream = %s, ", name);
        if (is_unbuffered(fp))
            printf("unbuffered");
        else if (is_linebuffered(fp))
            printf("line buffered");
        else
            printf("fully buffered");
        printf(", buffer size = %d\n", buffer_size(fp));
    }
    
    #if defined(_IO_UNBUFFERED)
    
    int is_unbuffered(FILE* fp){
        return(fp->_flags & _IO_UNBUFFERED);
    }
    
    int is_linebuffered(FILE *fp){
        return(fp->_flags & _IO_LINE_BUF);
    }
    
    int buffer_size(FILE *fp){
        return(fp->_IO_buf_end - fp->_IO_buf_base);
    }
    
    #elif defined(__SNBF)
    
    int is_unbuffered(FILE *fp) {
        return (fp->_flags & __SNBF);
    }
    
    int is_linebuffered(FILE *fp) {
        return (fp->_flags & __SLBF);
    }
    
    int buffer_size(FILE *fp) {
        return (fp->_bf._size);
    }
    
    #elif defined(__IONBF)
    
    //some
    
    #else
    #error unknown stdio implementation!
    #endif
    
    #include <apue.h>
    #include <error.h>
    
    int main(void) {
        char name[L_tmpnam], line[MAXLINE];
        FILE *fp;
        printf("%s\n", name);
        if ((fp = tmpfile()) == NULL)
            err_sys("tmpfile error");
        fputs("one line of output\n", fp);
        rewind(fp);
        if (fgets(line, sizeof(line), fp) == NULL)
            err_sys("fgets error");
        fputs(line, stdout);
        exit(0);
    }
    
    #include <apue.h>
    #include <error.h>
    
    void make_temp(char *template);
    
    int main() {
        char good_template[] = "/tmp/dirXXXXXX";
        char *bad_template = "/tmp/dirXXXXXX";
        printf("trying to create first temp file ...\n");
        make_temp(good_template);
        printf("trying to create second temp file ...\n");
        make_temp(bad_template);
        exit(0);
    }
    
    void make_temp(char *template) {
        int fd;
        struct stat sbuf;
        if ((fd = mkstemp(template)) < 0)
            err_sys("can't create temp file");
        printf("temp name = %s\n", template);
        close(fd);
        if (stat(template, &sbuf) < 0) {
            if (errno == ENOENT)
                printf("file doesn't exist\n");
            else
                err_sys("stat failed");
    
        } else {
            printf("file exists\n");
            unlink(template);
        }
    }
    
    #include <apue.h>
    #include <error.h>
    
    #define BSZ 48
    
    int main() {
        FILE *fp;
        char buf[BSZ];
    
        memset(buf, 'a', BSZ - 2);
        buf[BSZ - 2] = '\0';
        buf[BSZ - 1] = 'X';
        if ((fp = fmemopen(buf, BSZ, "w+")) == NULL)
            err_sys("fmemopen failed");
        printf("initial buffer contents: %s\n", buf);
        fprintf(fp, "hello word");
        printf("before flush: %s\n", buf);
        fflush(fp);
        printf("after fflush: %s\n", buf);
        printf("len of string in buf = %ld\n", (long) strlen(buf));
    
        memset(buf, 'b', BSZ - 2);
        buf[BSZ - 2] = '\0';
        buf[BSZ - 1] = 'X';
        if ((fp = fmemopen(buf, BSZ, "w+")) == NULL)
            err_sys("fmemopen failed");
        printf("initial buffer contents: %s\n", buf);
        fprintf(fp, "hello word");
        fseek(fp, 0, SEEK_SET);
        printf("after fseek: %s\n", buf);
        printf("len of string in buf = %ld\n", (long) strlen(buf));
    
        memset(buf, 'c', BSZ - 2);
        buf[BSZ - 2] = '\0';
        buf[BSZ - 1] = 'X';
        if ((fp = fmemopen(buf, BSZ, "w+")) == NULL)
            err_sys("fmemopen failed");
        printf("initial buffer contents: %s\n", buf);
        fprintf(fp, "hello word");
        fclose(fp);
        printf("after fclose: %s\n", buf);
        printf("len of string in buf = %ld\n", (long) strlen(buf));
    }
    

    相关文章

      网友评论

          本文标题:标准I/O库

          本文链接:https://www.haomeiwen.com/subject/rxaofktx.html