Linux文件编程,时间编程——系统调用-文件访问

15 篇文章 0 订阅

1、int creat(const char* pathname,mode_t mode)

功能:创建一个文件并以只写的方式打开。如果原来该文件存在,会将这个文件的长度截短为0。

若函数执行成功则返回打开文件的描述符,出错返回-1并设置errno。

pathname:要创建的文件名(包含路径,缺省为当前路径)

mode:创建模式

常见模式:

      宏表示:

      S_IRUSR     可读

      S_IWUSR     可写

      S_IXUSR      可执行

      S_IRWXU     可读可写可执行

      数字表示:

      可执行             1

      可写                 2

      可读                 4

      上面的值可以相加(1,2,4算法)来表示权限。

      可读写执行      7

      无任何权限      0

例子:

 * =====================================================================================
 *
 *       Filename:  2.1.cpp
 *
 *    Description:  创建文件
 *
 *        Version:  1.0
 *        Created:  2013年10月29日 18时21分19秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ydonghao (), ydonghao2@gmail.com
 *   Organization:  Personal
 *
 * =====================================================================================
 */

#include    <stdio.h>
#include    <stdlib.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include    <fcntl.h>

void	CreateFile(char* m_cpFilename)
{
    if( creat(m_cpFilename, 0753) < 0 )
    {
	printf("create file %s failure!\n", m_cpFilename);
    }
    else
    {
	printf("create file %s success!\n", m_cpFilename);
    }
}

int main(int argc, char* argv[])
{
    int i;
    if( argc < 2 )
    {
	perror("you haven't input the filename , please try again!\n");
	exit(EXIT_FAILURE);
    }

    for( i = 2 ; i < argc  ; i++ )
    {
	CreateFile(argv[i]);
    }
    
    return EXIT_SUCCESS;
}

creat(m_cpFilename, 0753)<0

753文字,7对应文件所有者,5对应文件所有组,3对应普通用户

编译链接以后,可以执行。

2、文件描述符

        在Linux系统中,所有开发的文件都对应一个文件描述符。文件描述符本质是一个非负整数,文件描述符的范围是0-OPEN_MAX。当打开一个文件时,由系统分配。现在大多系统OPEN_MAX=2014。

3、系统调用-打开

int open(const char* pathname, int flags)

int open(const char* pathname, int flags, mode_t mode)

pathname:要打开的文件名(包括路径,缺省我为当前路径)

flags:打开标志

         常见打开标志:

         O_RDONLY      只读打开

         O_WRONLY     只写打开

         O_RDWR          读写方式打开

         O_APPEND       追加方式打开

         O_CREAT         创建一个文件

         O_NOBLOCK    非阻塞方式打开

如果使用了O_CREAT 标志,则使用的函数是:

int open(const char* pathname, int flags, mode_t mode);

这时需要指定mode来表示文件的访问权限。

例子:

/*
 * =====================================================================================
 *
 *       Filename:  2.1.cpp
 *
 *    Description:  打开文件
 *
 *        Version:  1.0
 *        Created:  2013年10月29日 18时21分19秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  ydonghao (), ydonghao2@gmail.com
 *   Organization:  Personal
 *
 * =====================================================================================
 */

#include    <stdio.h>
#include    <stdlib.h>
#include    <sys/types.h>
#include    <sys/stat.h>
#include    <fcntl.h>

int main(int argc, char* argv[])
{
    int fd;
    if( argc < 2 )
    {
	perror("you haven't input the filename , please try again!\n");
	exit(EXIT_FAILURE);
    }

    if( (fd=open( argv[1], O_CREAT|O_RDWR , 0755)) < 0 )
    {
	perror("open file failurec\n");
	exit(EXIT_FAILURE);
    }
    else
    {
	printf("open file %d success!\n", fd);
    }
    close(fd);
    return EXIT_SUCCESS;
}

4、系统调用——关闭

当我们操作完文件以后,需要关闭文件:

int close(int)

fd:文件描述符,打开文件时,返回的文件描述符。

实例如上:

5、系统调用——读

int read(int fd, const void *buf, size_t length);

功能:

        从文件描述符fd所指定的文件中读取length个字节到buf所指向的缓冲区中。

        返回值为实际读取的字节数。

6、系统调用——写

int write(int fd,const void *buf, size_t length);

功能:

       把length个字节从buf指向的缓冲区中写到文件描述符fd所指向的文件中,

       返回值为实际写入的字节数。

7、系统调用——定位

int lseek(int fd, offset_t offset,int whence)

功能:

        将文件读写指针相对whence移动offset个字节。

 whence为下列其中一种:(SEEK_SET,SEEK_CUR和SEEK_END和依次为0,1和2).
 SEEK_SET 将读写位置指向文件头后再增加offset个位移量。
 SEEK_CUR 以目前的读写位置往后增加offset个位移量。
 SEEK_END 将读写位置指向文件尾后再增加offset个位移量。
 当whence 值为SEEK_CUR 或SEEK_END时,参数offet允许负值的出现。

返回值

当调用成功时则返回目前的读写位置,也就是返回文件指针相对文件头的位置。。若有错误则返回-1,errno 会存放错误代码。
可能设置erron的错误代码:
EBADF: fildes不是一个打开的文件描述符。
ESPIPE:文件描述符被分配到一个管道、套接字或FIFO。
EINVAL:whence取值不当。
下列是较特别的使用方式:
1) 欲将读写位置移到文件开头时:
lseek(int fildes,0,SEEK_SET);
2) 欲将读写位置移到文件尾时:
lseek(int fildes,0,SEEK_END);
3) 想要取得目前文件位置时:
lseek(int fildes,0,SEEK_CUR)
利用lseek计算文件长度

lseek(int fildes,0,SEEK_END);返回值就是文件长度。#include <stdio.h>

#include <stdlib.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>

int main(int argc,char *argv[])
{
    int fd,length;
    if(argc<2)
    {
       puts("Please input the open file pathname!\n");
      exit(1);
    }
    
    if((fd=open(argv[1],O_RDONLY))<0)
    {
     perror("Open file failure!");
     exit(1);
    }

    if((length = lseek(fd,0,SEEK_END))<0)
    {
       perror("lseek file failure!");
    }
        
    printf("The file's length is %d\n",length);
    close(fd);
    exit(0);
}

8、系统调用——访问判断

int access(const char* pathname, mode_z mode)

头文件:io.h(linux中为<unistd.h>)

pathname:文件名称

mode:要判断的访问权限。可以取以下值或者是他们的组合。

          R_OK:文件可读

          W_OK:文件可写

          X_OK:文件可执行

   F_OK:文件存在

返回值:执行成功返回0,否则如果一个条件不符合时,返回-1

例子:

#include     <unistd.h>

int main(void)
{
        if(access("/etc/password",R_OK));
        {
                 printf("'/etc/password' can be read!\n");
        }
}

9、file_cp.c

#include    <stdio.h>
#include    <sys/stat.h>
#include    <sys/types.h>
#include    <fcntl.h>
#include    <errno.h>

#define BUFFER_SIZE 1024 

int main(int argc,char **argv) 
{ 
    int from_fd;
    int to_fd; 
    int BytesWrite,BytesRead;
    char buffer[BUFFER_SIZE]; 
    char *ptr; 
	
    /*判断入参*/
    if(argc!=3) 
    { 
    	fprintf(stderr,"Usage:%s fromfile tofile\n",argv[0]); 
    	exit(1); 
    } 

    /* 打开源文件 */ 
    if((from_fd=open(argv[1], O_RDONLY)) == -1) 
    { 
	fprintf(stderr,"Open %s Error\n",argv[1], strerror(errno)); 
	exit(1); 
    } 

    /* 创建目的文件 */ 
    if((to_fd=open(argv[2],O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) == -1) 
    { 
	fprintf(stderr,"Open %s Error\n",argv[2],strerror(errno)); 
	exit(1); 
    } 

    /*进行文件拷贝*/
    while(BytesRead=read(from_fd, buffer, BUFFER_SIZE )) 
    { 
	//一个致命的错误发生了
	if( (BytesRead == -1) && (errno != EINTR ) ) 
	{
	    break;
	}
	else if(BytesRead > 0) 
	{
	    ptr = buffer;
	    while(  BytesWrite = write(to_fd, ptr, BytesRead ) )
	    {
		if( (BytesWrite == -1)&&( errno != EINTR) )
		{
		    break;
		}
		else if( BytesWrite == BytesRead )
		{
		    break;
		}
		else if( BytesWrite > 0 )
		{
		    ptr += BytesWrite;
		    BytesRead -= BytesWrite;
		}
	    }
	    //发生错误。
	    if( BytesWrite == -1 )
	    {
		break;
	    }
	}
    } 
    close(from_fd); 
    close(to_fd); 
    
    return 0;
} 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值