zoukankan      html  css  js  c++  java
  • unix -输入和输出

    • 不带缓冲的I/O
      函数open, read, write, lseek,close.
      这些函数都使用文件描述符。
      系统调用

    代码实现: 从标准输入读,并写入标准输出

    #include <stdio.h>          
    #include <stdlib.h>        
    #include <stddef.h>        
    #include <string.h>         
    #include <unistd.h>      
    
    
    #define  BUFFSIZE 4096
    
    int 
    main (int argc,  char *argv[])
    {
        int n;
        char buf[BUFFSIZE];
        
        while((n=read(STDIN_FILENO, buf, BUFFSIZE))>0)
        {
            if(write(STDOUT_FILENO, buf, n)!=n)
            {
                printf("write error
    ");
                exit(1);
            }
        }
    
        if(n<0)
        {
            printf("read error
    ");
            exit(1);        
        }
    
        exit(0);
    }
    

    标准函数库,带缓冲I/O接口
    getc, putc

    #include <stdio.h>          
    #include <stdlib.h>        
    #include <stddef.h>        
    #include <string.h>         
    #include <unistd.h>      
    
    
    
    int 
    main (int argc,  char *argv[])
    {
        int c;
        
        while((c=getc(stdin))!=EOF)
        {
            if(putc(c,stdout)==EOF)
            {
                printf("output error
    ");
                exit(1);
            }
        }
    
        if(ferror(stdin))
        {
            printf("input error
    ");
            exit(1);        
        }
    
        exit(0);
    }
    
  • 相关阅读:
    Animation(三)
    布局
    AutoCommpleteText
    PHP数组中常用函数
    Animation(四)
    转:JAVA内存映射文件
    Ubuntu安装jdk
    转:Java NIO 详解
    转:长连接与短连接
    Direct or Nondirect ByteBuffer
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15361531.html
Copyright © 2011-2022 走看看