有关LINUX I/O 的一些学习笔记

今天在看一些关于LINUX下编程的电子书籍,把所学到的一些知识列一下:好记性不如烂笔头嘛

 

无缓冲I/O

Open,read,write,lseekclose函数提供无缓冲I/O。这些函数都对文件奏效。

 

句子比较简单就不翻译了,免得闹笑话,以下是原文。

The constants STDIN_FILENO and STDOUT_FILENO are defined in <unistd.h> and specify the file descriptors for standard input and standard output. These values are typically 0 and 1, respectively, but we'll use the new names for portability.

 

EXAMPLE

#include "apue.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)
            err_sys("write error");
        if (n < 0)
            err_sys("read error");

  
  
   
    
  
  
        exit(0);
}

If we compile the program into the standard name (a.out) and execute it as

   ./a.out > data

 

标准I/O

标准I/O函数提供给一个无buffer I/O函数一个带buffer 的接口,这样就不要担心如何选择最佳的buffer大小

标准I/O用常量stdin,stdou来指定标准输入和标准输出

附上小例子一个

#include "apue.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);
}

 

都是一些比较初级的东西,不过也不是所有人都知道,例如我。哈哈,惭愧,惭愧

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值