文件在Linux中起着很重要的作用, Linux将所有的设备都看作是文件,包括标准的输入输出。要打开一个文件, 可以用open,使用完成之后,我们可以用close来关闭文件。
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
int open( char *pathname ,int flag )
int open( char *pathname , int flag ,mode_t mode)
int close(int fd)
open函数有两个形式.其中pathname是我们要打开的文件名(包含路径名称,缺省是认为在当前路径下面).flag可以去下面的一个值或者是几个值的组合.
O_RDONLY:以只读的方式打开文件.
O_WRONLY:以只写的方式打开文件.
O_RDWR:以读写的方式打开文件.
O_APPEND:以追加的方式打开文件.
O_CREAT:创建一个文件.
O_EXEC:如果使用了O_CREAT而且文件已经存在,就会发生一个错误.
O_NOBLOCK:以非阻塞的方式打开一个文件.
O_TRUNC:如果文件已经存在,则删除文件的内容.
-----------------------------------------------------------------
前面三个标志只能使用任意的一个.如果使用了O_CREATE标志,那么我们要使用open的第二种形式.还要指定mode标志,用来表示文件的访问权限.mode可以是以下情况的组合.
S_IRUSR 用户可以读 S_IWUSR 用户可以写
S_IXUSR 用户可以执行 S_IRWXU 用户可以读写执行
-----------------------------------------------------------------
S_IRGRP 组可以读 S_IWGRP 组可以写
S_IXGRP 组可以执行 S_IRWXG 组可以读写执行
-----------------------------------------------------------------
S_IROTH 其他人可以读 S_IWOTH 其他人可以写
S_IXOTH 其他人可以执行 S_IRWXO 其他人可以读写执行
-----------------------------------------------------------------
S_ISUID 设置用户执行ID S_ISGID 设置组的执行ID
-----------------------------------------------------------------
我们也可以用数字来代表各个位的标志.Linux总共用5个数字来表示文件的各种权限.
00000.第一位表示设置用户ID.第二位表示设置组ID,第三位表示用户自己的权限位,第四位表示组的权限,最后一位表示其他人的权限.
每个数字可以取1(执行权限),2(写权限),4(读权限),0(什么也没有)或者是这几个值的和.
比如我们要创建一个用户读写执行,组没有权限,其他人读执行的文件.设置用户ID位那么我们可以使用的模式是--1(设置用户ID)0(组没有设置)7(1+2+4)0(没有权限,使用缺省)5(1+4)即10705:
open("temp",O_CREAT,10705);
如果我们打开文件成功,open会返回一个文件描述符.我们以后对文件的所有操作就可以对这个文件描述符进行操作了.
当我们操作完成以后,我们要关闭文件了,只要调用close就可以了,其中fd是我们要关闭的文件描述符.
Linux提供了读写文件,open , write
#include<unistd.h>
int read(int fd,char *buffer , int len)
int write(int fd, char *buffer , int len)
其中的fd是我们要写入的文件的描述符, buffer存放读写的内容,len 是读写的长度。
读写成功是,返回读写的实际大小,若是读到文件的末尾,则返回0 , 若是遇到了中断信号,则返回-1.
/*
文件的创建和读写
*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdlib.h>
#define MAXN 1024
int main(int argc , char **argv){
int from_fd ,to_fd ;
int byte_read,byte_write ;
char buffer[MAXN] ;
char *ptr ;
if(argc != 3){
fprintf(stderr, "Usage : %s from file to file\n",argv[0]);
_exit(EXIT_FAILURE) ;
}
if(-1 == (from_fd = open(argv[1] , O_RDONLY)) ){
/*读文件打开失败*/
fprintf(stderr,"Fail to open the %s file!\n",argv[1]);
_exit(EXIT_FAILURE);
}
if( -1 == ( to_fd = open(argv[2], O_WRONLY|O_CREAT , S_IRUSR|S_IWUSR ))){
fprintf(stderr , "Fail to open the destination file:%s!\n",argv[2]);
_exit(EXIT_FAILURE);
}
while( byte_read = read(from_fd , buffer , MAXN) ){
/*读取文件错误*/
if(byte_read == -1) break ;
/*读入byte_read个大小的文件内容*/
else if(byte_read > 0){
ptr = buffer ;
/*写入to文件中去*/
while( byte_write = write(to_fd , ptr, byte_read)){
if(byte_write == -1) break ;
else if(byte_write == byte_read){
/*已经全部都写完了*/
break ;
}
else{
/*只是写了一部分*/
byte_read -= byte_write ;
ptr += byte_write ;
}
}
if(byte_write == -1) break ;
}
}
close(from_fd) ;
close(to_fd) ;
return 0;
}
管道文件
Linux提供的管道文件用pipe函数创建
#include<unistd.h>
int pipe( int fd[2] )
pipe调用可以创建一个管道(通信缓冲区).当调用成功时,我们可以访问文件描述符fd[0],fd[1].其中fd[0]是用来读的文件描述符,而fd[1]是用来写的文件描述符.
在实际使用中我们是通过创建一个子进程,然后一个进程写,一个进程读来使用的
/*
管道文件
父进程读,子进程写。
*/
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#define MAXN 1024
int main(int argc, char **argv){
int fd[2],num;
char buffer[MAXN] ;
pid_t pid ;
if(argc!=2){
printf("Usage: filename Error\n");
_exit(1) ;
}
if( pipe(fd) != 0){
fprintf(stderr,"Pipe Error!!\n");
_exit(1) ;
}
pid = fork() ;
if(pid < 0){
fprintf(stderr , "Creat pid Error!!\n"); _exit(1) ;
}
if(pid == 0){
close(fd[0]) ;
printf("Child[%d] is Writing to pipe!\n",getpid());
sleep(3) ;
write(fd[1] , argv[1] , strlen(argv[1]));
_exit(0) ;
}
else{
close(fd[1]) ;
printf("Parent [%d] is reading from the pipe!\n",getpid());
memset(buffer , 0 , sizeof(buffer));
num = read(fd[0] , buffer , MAXN) ;
printf("%d\n",num);
printf("Parent [%d] read : %s\n",getpid() , buffer);
_exit(0) ;
}
}
我们可以用dup2函数实现输出的重定向
#include<unistd.h>
int dup2(int oldfd , int newfd)
其中的newfd是oldfd的复制 ,即newfd 将指向 oldfd指向的那个文件
/*
文件重定向
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#define MAXN 1024
/*
将一个标准输出重定向到一个文件
*/
int main(int argc, char **argv){
int fd ;
char buffer[MAXN] ;
if(argc != 2){
fprintf(stderr, "Usagre:%s outfile\n",argv[0]);
_exit(1) ;
}
if( (fd=open(argv[1] , O_WRONLY|O_CREAT|O_TRUNC , S_IWUSR|S_IRUSR|S_IRWXG))==-1){
fprintf(stderr, "Open %s Error\n",argv[1]);
_exit(1) ;
}
if(dup2(fd, STDOUT_FILENO) ==-1){ /*将标准输出重定向到一个文件*/
fprintf(stderr,"Redirect Standard Out Error\n");
_exit(1) ;
}
fprintf(stderr, "Now please input string!\n");
fprintf(stderr, "(To quit use CTRL+D)\n");
while(1){
fgets(buffer,MAXN,stdin) ; /*从标准输入上读入内容,存放在buffer中*/
if(feof(stdin)) break ; /*结束输入*/
write(STDOUT_FILENO,buffer,strlen(buffer)) ; /*通过重定向写入到文件*/
}
return 0;
}