Linux C——API文件操作函数

一、有哪些API函数

1.creat

函数的作用: 创建一个文件;

函数的原型: int  creat(const char *pathname, mode_t mode);

文件头:  #include <sys/types.h>

        #include <sys/stat.h>

        #include <fcntl.h>

返回值:成功:新的文件描述符;

         出错:  -1

         

     

2. open

函数的作用:打开一个文件;

函数的原型:

int  open(const char *pahtname, int flags);

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

返回值:文件描述符---成功;出错:-1

flags

参数:

O_RDONLY

  O_WRONLY

O_RDWR

        O_CREAT:  如果原来折耳根文件不存在,那么有这个参数就可以创建这个文件;

     O_APPEND:原来有内容,则会自动保留文件内容,自动向下读写;

     O_TRUNC:  文件存在,有内容,文件清空;   

 

3. read

函数的作用: 从打开的文件中读取数据

函数的原型:ssize_t  read(int fd, void *buf,  size_t count);

包含的头文件: #include  <unistd.h>

  

返回值:正常是实际读到的字节数;

        如果是在文件结束或者是无数据,返回0

        出错,-1

 

4. write

函数的作用: 向打开的文件中写数据

函数的原型: ssize_t   write(int fd, const void *buf, size_t count);

头文件:  #include <unistd.h>

    

返回值:  成功会返回实际写入的字节数;

          出错:-1

          

5.lseek

函数的功能:进行文件定位

函数的原型:  int lseek(int fd, offset_t  offset, int whence);

函数的参数:fd

            offset: 指针的微调,在指定的指针向前移动为负, 向后为正;

            whenceSEEK_SET:放在文件头

             SEEK_CUR:当前的位置;

                 SEEK_END:  文件尾;

             

返回值:返回文件当前指针到文件开始的地方有多少字节;

       出错-1

 

二、如何使用API函数

1.各种函数的使用实例

①用creat函数创建文件的实例:

#include <stdio.h>

#include <stdlib.h>

 

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

 

void  create_file(char *filename)

{

    if(creat(filename,0755)<0){

        printf("create file %s failure!\n",filename);

        exit(EXIT_FAILURE);

    }else{

        printf("create file %s success!\n",filename);

    }

}

 

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=1;i<argc;i++)

    {

        create_file(argv[i]);    

    }

 

    exit(EXIT_SUCCESS);

}

open函数运用的实例:

#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)

    {

        puts("please input the open file pathname!\n");

        exit(1);

    }

    

    //如果flag参数里有O_CREAT表示,该文件如果不存在,系统则会创建该文件,该文件的权限由第三个参数决定,此处为0755

    //如果flah参数里没有O_CREAT参数,则第三个参数不起作用.此时,如果要打开的文件不存在,则会报错.

    //所以fd=open(argv[1],O_RDWR),仅仅只是打开指定文件

    if((fd=open(argv[1],O_CREAT|O_RDWR,0755))<0)

    {

        perror("open file failure!\n");

        exit(1);

    }else{

        printf("open file %d  success!\n",fd);

 

    }

    close(fd);

    exit(0);

    

}

 

③文件读写的实例:

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <stdio.h>

#include <unistd.h>

 

 

#define LENGTH 100

main()

{

 int fd, len;

 char str[LENGTH];

 

 fd = open("hello.txt", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); /* 创建并打开文件 */

 

 if (fd > 0)

 {

  write(fd, "Hello, Software Weekly", strlen("Hello, software weekly"));      /* 写入 Hello, software weekly字符串 */

  close(fd);

 }

 

 fd = open("hello.txt", O_RDWR);

 len = read(fd, str, LENGTH);       /* 读取文件内容 */

 str[len] = '\0';

 printf("%s\n", str);

 close(fd);

}

 

2.文件的创建有哪些方法?

共有三种方法:

①使用creat函数

②使用open函数,使用带有三个参数的形式

③使用fopen函数,模式选择w或者a

 

3.如何使用文件API操作进行文件复制

#include <string.h>

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

 

#define BUFFER_SIZE 1024

 

int main(int argc,char **argv)

{

FILE *from_fd;

FILE *to_fd;

long file_len=0;

char buffer[BUFFER_SIZE];

char *ptr;

/*判断入参*/

if(argc!=3)

{

printf("Usage:%s fromfile tofile\n",argv[0]);

exit(1);

}

 

/* 打开源文件 */

if((from_fd=fopen(argv[1],"rb"))==NULL)

{

printf("Open %s Error\n",argv[1]);

exit(1);

}

 

/* 创建目的文件 */

if((to_fd=fopen(argv[2],"wb"))==NULL)

{

printf("Open %s Error\n",argv[2]);

exit(1);

}

 

/*测得文件大小*/

fseek(from_fd,0L,SEEK_END);

file_len=ftell(from_fd);

fseek(from_fd,0L,SEEK_SET);

printf("from file size is=%d\n",file_len);

 

/*进行文件拷贝*/

while(!feof(from_fd))

{

fread(buffer,BUFFER_SIZE,1,from_fd);

if(BUFFER_SIZE>=file_len)

{

fwrite(buffer,file_len,1,to_fd);

}

else

{

fwrite(buffer,BUFFER_SIZE,1,to_fd);

file_len=file_len-BUFFER_SIZE;

}

memset(buffer, 0, BUFFER_SIZE);

// bzero(buffer,BUFFER_SIZE); //清零

}

fclose(from_fd);

fclose(to_fd);

exit(0);

}

 

4.如何判断文件的大小?

可以使用lseek函数,把指针移到文件末尾,返回值就是文件的大小

Int length = lseek(fd,SEEK_END,0);

Length就是文件的长度

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值