设备->内存(输入操做)linux
内存->设备(输出操做)ide
ANSI C提供的标准I/O库称为高级I/O,一般也称为带缓冲的I/O函数
一般也称为不带缓冲的I/O测试
2. 文件描述符:fdspa
3.文件描述符与文件指针相互转换3d
能够经过如下两个函数实现:指针
#include <stdio.h>code
int fileno(FILE *stream)blog
测试程序:进程
#include <stdlib.h> #include <stdio.h> int main(void) { printf("fileno(stdin) = %d\n", fileno(stdin)); printf("fileno(stdout) = %d\n", fileno(stdout)); printf("fileno(stderr) = %d\n", fileno(stderr)); return 0; }测试结果:
#include <stdio.h>
FILE *fdopen(int fd, const char *mode) //mode :r,w,r+,w+,a,a+
4.文件系统调用
有几种方法能够得到容许访问文件的文件描述符。最经常使用的是使用open()(打开)系统调用
函数原型
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);参数
path :文件的名称,能够包含(绝对和相对)路径
flags:文件打开模式
mode:用来规定对该文件的全部者,文件的用户组及系 统中其余用户的访问权限
返回值
打开成功,返回文件描述符;
打开失败,返回-1
文件打开方式:
O_EXCL表示:当O_EXCL|O_CREAT时,若文件存在,则打开失败,不存在,则打开成功
访问权限:
open系统调用的几点说明:
能够利用按位逻辑加(bitwise-OR)(|)对打开方式的标志值进行组合。
如打开一个新文件:
#define NEWFILE (O_WRONLY|O_CREAT|O_TRUNC)
对访问权限位进行访问所用到的标识符,都可以经过
#include <sys/stat.h> 访问到,一样能够经过|运算来对访问权限进行组合也能够直接给出数字表示如0655
#define MODE755 (S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
注:文件的访问权限是根据:umask&~mode得出来的,例如umask=0022,mode = 0655 则访问权限为:644
测试程序:
#include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h> #define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } while(0) int main(void) { umask(0); int fd; fd = open("test.txt", O_WRONLY | O_CREAT, 0666); if (fd == -1) ERR_EXIT("open error"); printf("open succ\n"); return 0; }测试结果一:采用默认的umask值
测试结果二:从新设置umask值
为了从新利用文件描述符,用close()系统调用释放打开的文件描述符
函数原型:
#include <unistd.h>
int close(int fd);
函数参数:
-fd :要关闭的文件的文件描述符
返回值
若是出现错误,返回-1
调用成功返回0
注:若没有显示调用close(),当程序退出时也会关闭文件
为了维持与早期的UNIX系统的向后兼容性,Linux也提供可选的建立文件的系统调用,它称为creat()。现代的linux内核不多采用creat建立文件,由于open能够完成建立功能
函数原型:
int creat(const char *path, mode_t mode);
参数
path :文件的名称,能够包含(绝对和相对)路径
mode: 用来规定对该文件的全部者,文件的用户组及系 统中其余用户的访问权限
返回值
打开成功,返回文件描述符;
打开失败,返回-1
在UNIX的早期版本中,open()系统调用仅仅存在两个参数的形式。如文件不存在,它就不能打开这些文件。文件的建立则由单独的系统调用creat()完成。在Linux及全部UNIX的近代版本中,creat()系统调用是多余的。
creat()调用
fd = creat(file, mode);
彻底等价于近代的open()调用
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, mode);