C语言 底层IO read&write

基础

  1. #include<unistd.h>
    2.win下没有unistd.h要替换成:
#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */

read

  1. ssize_t read(int fd,void*buf,size_t count)
  2. 参数
    参数|描述
    –|--
    fd|默认为一般为3,0,1,2是stdin,stdout,stderr
    buf|要读入的数据
    count|读入的字节数,读后位置后移

3.返回
成功:返回读出的字节数
失败:返回-1

write

  1. ssize_t write(int fd,const void*buf,size_t count);
  2. 参数
    参数|描述
    –|--
    fd|默认为一般为3,0,1,2是stdin,stdout,stderr
    buf|要写出的数据
    count|是每次写出的字节数
    3.返回
    成功:返回写出的字节数
    失败:返回-1

dup

#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */
//#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>


int main()
{
	int fd;
	fd = open("./test.txt", O_WRONLY | O_TRUNC | O_CREAT);
	close(1); //关闭文件描述符1,既stdout
	dup2(fd, 1); //将文件描述符1,对应的位置输出指向换成fd对应的位置输出指向
	printf("Hello World!\n"); //把请按任意键继续都打印进去了
	system("pause");
}

  1. 初始化进程,默认0,1,2,对应stdin,stdout,stderr,都输出到屏幕

在这里插入图片描述

  1. fd=open(...),新打开一个文件描述符,指向文件

在这里插入图片描述

  1. close(1),将文件描述符1对应的指向删除,此时对1做任何操作都无效

在这里插入图片描述

  1. dup2(fd, 1);,将fd的指向复制给1,所以此时对1的输出都写入到文件里
    在这里插入图片描述
  2. printf默认的输出文件描述符是1,所以输出任何东西都会定向到文件里

例子

#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */
//#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>


int main()
{
	char wbuf[] = "wang";
	char rbuf[] = "zhao";
	int fd = open("test.txt", O_RDWR |O_CREAT);
	printf("%d\n", fd);
	write(fd, wbuf, 4);

	read(fd, rbuf, 4);
	printf("%s\n", rbuf);
	system("pause");
}

参考:
http://www.gnu.org/software/libc/manual

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值