zoukankan      html  css  js  c++  java
  • unix的输入输出操作

    unix的输入输出操作


    使用的头文件
    #include <unistd.h>
    #include <stdio.h>

    函数说明
    • ssize_t read(int fd, void *buf, size_t count);
    从fd 中最多读入 count 个信息到 buf 中。当 fd 的为 STDIN_FILENO 这个宏定义的时候,表示标准输入。
    • ssize_t write(int fd, const void *buf, size_t count);
    将最多 count 个信息从 buf 中写道 fd 所指向的文件中,当 fd 的为 STDOUT_FILENO 这个宏定义的时候,表示标准输出。
    • int getc(FILE *stream);
    从 stream 中取得一个字符,并将此字符返回给返回值。当 stream 是变量stdin 时,表示标准输入,stdin 在 stdio.h 中有定义。
    • int putc(int c, FILE *stream);
    将字符 c 保存到 stream 中。当 stream 是变量 stdout 的时候,表示标准输出,stdout 在 stdio.h 中有定义。
    • char *fgets(char *s, int size, FILE *stream);
    从 stream 中取得 size 个字符,并将其保存在 s 中,并返回指向 s的指针。
     
    程序举例:
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    #define     BUFFSIZE     4096
    
    int main(void)
    {
        int n;
        char buf[BUFFSIZE];
        
        while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
            if (write(STDOUT_FILENO, buf, n) != n) printf("write error");
        
        if (n < 0) printf("read error");
        
        exit(0);
    }

    或者

    #include <stdio.h>
    
    int main(void)
    {
        int c;
        
        while ((c = getc(stdin)) != EOF)
            if (putc(c, stdout) == EOF) printf("output error");
        
        if (ferror(stdin)) printf("input error");
        
        exit(0);
    }
  • 相关阅读:
    WC命令
    dcoker machine
    linux命令
    Valgrind 检测程序内存使用
    golang flag
    面试之---二叉树的遍历
    FFMpeg 版本错误
    C++中namespace的使用
    QT之QStatusBar
    建立ftp服务器和客户端
  • 原文地址:https://www.cnblogs.com/babyha/p/5209425.html
Copyright © 2011-2022 走看看