Linux文件操作函数(一)

本文详细介绍了C语言中基本的文件操作,包括使用open()函数打开文件,read()和write()进行读写操作,以及close()关闭文件的实例。通过创建一个简单的CP命令模拟,展示了这些函数的实际应用。
摘要由CSDN通过智能技术生成

文件操作函数

这一节主要讲解最基础的打开文件,读写文件,关闭文件三个函数吗,以及其对对应的demo例程。

目录

文件操作函数

打开文件

 DEMO

 读写文件

关闭文件

函数例程

DEMO


        —open函数

            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_CREAT 创建 O_APPEND 文件结尾 O_TRUNC 截断文件 O_NONBLOCK 非阻塞

            mode—创建的文件权限  umask & mode

            成功返回文件描述符(整数),失败返回 -1 并设置 errno   

 DEMO

#include <stdio.h>      //Standrad c program library head file
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{

        int fd; //file descriptor

        fd = open("file",O_RDWR);       //Open the file to read/write

        if(fd == -1 ){

                fd = open("file",O_RDWR|O_CREAT,0641);  //If file not exist, creat file
                if(fd == -1){

                        perror("open errno");   //if creat file failed, printf mistake
                        exit(1);
                }

                printf("creat success! file fd = %d\n",fd);     //creat file success, printf file descriptor 
        }else{

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

        close(fd); //close file

        return 0;
}

 读写文件

     —read/write函数

            ssize_t read(int fd, void *buf, size_t count)

            ssize_t write(int fd, const void *buf, size_t count)

            fd—文件描述符(open文件成功的返回值) 

            buf—读/写缓冲区

            count—读/写文件的大小

            成功返回读/写文件的字节数,返回0表示读/写完,失败返回 -1 并设置 errno   

光标定位

—lseek函数

            off_t lseek(int fd, off_t offset, int whence);

            fd—文件描述符(open文件成功的返回值) 

            offset—文件相对偏移量

            whence— 偏移起始位置,SEEK_SET—从头偏移 SEEK_CUR—从当前位置偏移
            SEEK_END—从文件末尾偏移   

            成功返回,返回从文件头偏移的字节数,失败返回 -1 并设置 errno   

 DEMO

#include <stdio.h>      //Standrad c program library head file
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/*
 *Functional performance: open file function 
 *Return value: on seccess return file descriptor, failed return -1
 */

int openFile(const char *pathname)
{
        int fd;
        fd = open(pathname,O_RDWR);     //Open the file to read/write
        if(fd == -1 ){
                perror("open errno");   //if open file failed, printf mistake
                return -1;
        }else{

                return fd;
        }
}

int main(int argc, char *argv[])
{
        int fd; //file descriptor

        char *str = "heelo,word!";

        char buf[1024] = {'\0'};

        fd = openFile("file");  //open file

        int n_write = write(fd,str,strlen(str));    //write str to an open file 

        if(n_write == -1){

                perror("write errno");  //write file failed     
                exit(1);
        }

        printf("write file success!\n");        //write file success

        /*
        close(fd); //Upon write suceess close file
        fd = openFile("file");  //open the file from a new one
        */
        lseek(fd,-n_write,SEEK_CUR);    //让光标从新定位到文件开头

        int n_read = read(fd,buf,n_write);

        if(n_read == -1){

                perror("read errno");
                exit(1);

        }

        printf("file size:%d file data:%s\n",n_read,buf);       //printf file size and content's 

        close(fd); //close file

        return 0;
}

 关闭文件

—close函数

            int close(int fd)

            fd—文件描述符(open文件成功的返回值) 

            成功返回 0,失败返回 -1 并设置 errno 

函数例程

        由过以上三个文件操作函数我们来编写一个demo实现cp-shell指令功能

DEMO

/*
 *=mycp=
 */
#include <stdio.h>      //Standrad c program library head file
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

/*
 *Functional performance: open file function 
 *Return value: on seccess return file descriptor, failed return -1
 */

int openFile(const char *pathname)
{
        int fd;
        fd = open(pathname,O_RDWR|O_CREAT,0600);        //open file
        if(fd == -1){

                perror("open errno");   //if open file failed, printf mistake
                return -1;
        }else{

                return fd;
        }
}

int main(int argc, char *argv[])
{

        int fdsrc; //source file descriptor
        int fddes; //target file descriptor

        char buf[4096] = {'\0'}; //cache

        int num_read; //read source file size
        int num_write; //write target file size

        if(argc != 3){

                printf("You enter parameter is mistake!\n");    //parameter error
                exit(0);
        }

        fdsrc = openFile(argv[1]);      //open source file
        fddes = openFile(argv[2]);      //open target file

        while(num_read = read(fdsrc,buf,sizeof(buf))){

                num_write = write(fddes,buf,num_read);  //copy contents of the soucre file, write to target file

                if(num_write == -1){

                        perror("write error");
                        exit(0);
                }
        }

        close(fdsrc); //close source file                       
        close(fddes); //close target file

        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值