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);
    }
    
  • 相关阅读:
    [.NET学习]抽象&#
    几个国内开源
    通过C#命令行&#
    我的宝贝
    我的新博客
    常用的在线网
    收集一些.NET开
    论研究生学术
    在vs2008里安装使&#
    c#编码好习惯
  • 原文地址:https://www.cnblogs.com/feiwatson/p/15361531.html
Copyright © 2011-2022 走看看