一、文件描述符
函数名称:fileno
函数原型:int _fileno( FILE *stream );
函数功能:用来取得指定的文件流所使用的文件描述符
返回值:某个数据流的文件句柄
头文件:stdio.h
demo:
#include <stdio.h>
int main(){
printf("the file descriptor for stdin is %d\n",fileno(stdin));
printf("the file descriptor for stdout is %d\n",fileno(stdout));
printf("the file descriptor for stderr is %d\n",fileno(stderr));
return 0;
}
二、open函数
函数名称:open
函数原型:int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
函数功能:打开或创建一个文件
返回值: 成功则返回文件描述符,否则返回 -1.(open返回的文件描述符一定是最小的未被使用的描述符。)
头文件:#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
参数:1、pathname 是要打开或创建文件的名字
2、oflag用于指定文件的打开/创建模式,这个参数可由以下常量(定义于 fcntl.h)通过逻辑或构成。
oflag的值:
打开/创建文件时,至少得使用下述三个常量中的一个:
O_RDONLY
O_WRONLY
O_RDWR
以下常量是选用的:
O_APPEND
O_CREAT
O_EXCL
O_TRUNC
O_NOCTTY
O_NONBLOCK
以下三个常量同样是选用的,它们用于同步输入输出:
O_DSYNC
O_RSYNC
O_SYNC
3、mode则有下列数种组合,只有在建立新文件时才会生效,此外真正建文件时的权限会受到umask值所影响,因此该文件权限应该为(mode-umaks).返回值 若所有欲核查的权限都通过了检查则返回0 值, 表示成功,只要有一个权限被禁止则返回-1。
S_IRWXU 00700 权限,代表该文件所有者具有可读、可写及可执行的权限。
S_IRUSR 或S_IREAD, 00400权限,代表该文件所有者具有可读取的权限。
S_IWUSR或S_IWRITE,00200 权限,代表该文件所有者具有可写入的权限。
S_IXUSR或S_IEXEC, 00100 权限,代表该文件所有者具有可执行的权限。
S_IRWXG 00070权限,代表该文件用户组具有可读、可写及可执行的权限。
S_IRGRP 00040 权限,代表该文件用户组具有可读的权限。
S_IWGRP 00020权限,代表该文件用户组具有可写入的权限。
S_IXGRP 00010 权限,代表该文件用户组具有可执行的权限。
S_IRWXO 00007权限,代表其他用户具有可读、可写及可执行的权限。
S_IROTH 00004 权限,代表其他用户具有可读的权限
S_IWOTH 00002权限,代表其他用户具有可写入的权限。
S_IXOTH 00001 权限,代表其他用户具有可执行的权限。
demo:
#include <unistd.h>
#include <sys/types.h> /*for open*/
#include <sys/stat.h> /*for open*/
#include <fcntl.h> /*for open*/
#include <stdlib.h> /*for perror exit */
#include <stdio.h> /*for perror*/
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(void)
{
umask(0);//为进程设置文件模式创建屏蔽字,设为0表示不屏弊
int fd;
fd = open("test.txt", O_WRONLY | O_CREAT, 666);//666表示所有者,所属组,其他用户都可以读写
if (fd == -1)
ERR_EXIT("open error");
printf("open succ\n");
return 0;
}
运行之后生成的文件:
三、read函数
函数名称:read
函数原型:ssize_t read(int fd, void *buf, size_t count);
函数功能:从fd指向的文件的当前偏移量至多读count个字节到buf中。成功返回写入buf的字节数。出错时则返回-1,
并设置errno。
返回值: 成功则返回文件描述符,否则返回 -1.(open返回的文件描述符一定是最小的未被使用的描述符。)
头文件: #include <unistd.h>
调用read后可能发生的情况:
调用返回一个等于count的值。结果和预期一致。
调用返回一个大于0小于count的值。一个信号打断了读取过程,或在读入count个字节前已达到EOF。
调用返回0.这标志着EOF。没有可以读的数据。
调用被阻塞,因为没有可以读取的数据。这在非阻塞模式下不会发生。
调用返回-1,并且errno被设置为EINTR。这表示在读入字节之前收到了一个信号。可以重新进行调用。
调用返回-1,并且errno被设置为EAGAIN。这表示读取会因没有可用的数据而阻塞,而读请求应该在之后重开。这只在非阻塞模式下发生。
调用返回-1,并且errno被设置不同于EINTR(系统调用被中断)或EAGAIN(重试)的值。这表示某种更严重的错误。
四、write函数
函数名称:write
函数原型:ssize_t write(int fd, const void *buf, size_t count);
函数功能:函数向打开的设备或文件中写数据.
返回值: 成功返回写入的字节数,出错返回-1并设置errno写常规文件时,write的返回值通常等于请求写的字节数
count,而向终端设备或网络写则不一定。
头文件: #include <unistd.h>
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
现在可以用open read write函数做一个小demo ,把一个文件的内容复制到另一个文件:
#include <stdio.h> /*for fprintf*/
#include <unistd.h> /*for read write*/
#include <sys/types.h>/*for open*/
#include <sys/stat.h> /*for open*/
#include <fcntl.h> /*for open*/
#include <stdlib.h> /*for EXIT_FAILURE*/
#define ERR_EXIT(m) \
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main(int argc,char* argv[]){
int infd;
int outfd;
if(argc!=3){
fprintf(stderr,"Usage %s src dest\n",argv[0]);
exit(EXIT_FAILURE);
}
infd = open(argv[1],O_RDONLY);
if(infd==-1){
ERR_EXIT("open src error");
}
if((outfd=open(argv[2],O_WRONLY|O_CREAT|O_TRUNC,644))==-1){
ERR_EXIT("open dest error");
}
char buf[1024];
int nread;
int nwrite;
while((nread=read(infd,buf,1024))>0){
if((nwrite=write(outfd,buf,nread))==-1){
ERR_EXIT("write error");
}
}
close(infd);
close(outfd);
return 0;
}