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 就是创建时候设置文件权限的。