2、文件描述符

2、文件描述符

#include<stdio.h>

#include<unistc.h>

Int main()

{

       write(1,”hello”,5); //1代表标准输出,代表要输出的字符串,5代表输出5个字符

       return0;

}

 

在c语言中

标准输入:STDIN_FILENO

标准输出:STDOUT_FILENO

标准错误:STDERR_FILENO

在文件IO下

标准输入:0

标准输出:1

标准错误:2



 

标准IO控制文件

FILE *fp = fopen (“a.txt”.”w”);//可写的方式打开一个文件

通过控制fp指针来控制具体的文件

 

文件IO控制文件

int fd = open(“a.txt”,xxx);

通过fd文件描述符来控制具体的文件

常用函数有:creat、open、read、write、lseek(文件定位)、close。

test.c

#include<stdio.h>

#include<sys/types.h>

#include<sys.stat.h>

#include<fcntl.h>

#include<unistd.h>

int main()

{

       intfd;

       fd= open(“a.txt”,O_RDWR | O_CREAT)    ;//如果文件不存在创建文件

       printf(“fd= %d\n”,fd);

       write(1,&fd,sizeof(fd));

       close(fd);

       return  0;

}

修改文件权限属性chmod   777 test.c

也可以在open函数使用的时候

int open(const char *pathname,intflags,mode_t mode);//mode 就是创建时候设置文件权限的。

 

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值