天天看点

Linux系统编程——文件

Linux系统编程——文件
Linux系统编程——文件

  1.文件打开及创建

函数原型

int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
/*RETURN VALUE
       open(), openat(), and creat() return the new file descriptor, or -1  if
       an error occurred (in which case, errno is set appropriately).*/
           
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
        int fd;
        fd = open("./file1",O_RDWR);
        if(fd < 0){
                printf("open file1 failed don't exist\n");
        }
        fd = open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("open file1 success\n");
        }
        close(fd);
        return 0;
}
           

2.文件写入操作

函数原型

ssize_t write(int fd, const void *buf, size_t count);
/*RETURN VALUE
On error, -1 is returned, and errno is set appropriately.*/
           

代码示例:

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

int main()
{
        int fd;
        int n_write;
        char readBuf[128] = {'\0'};
        char *buf = "Practice and review";
        fd = open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("open file1 success fd = %d\n",fd);
        }
        write(fd,buf,strlen(buf));
        printf("%d\n",n_write);
        close(fd);
        return 0;
}
           

3.文件读取操作

函数原型

#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
/*RETURN VALUE
On error, -1 is returned, and errno  is  set  appropriately.*/ 
           

代码示例:

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

int main()
{
        int fd;
        int n_write;
        char readBuf[128] = {'\0'};
        char *buf = "Practice and review";
        fd = open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("open file1 success fd = %d\n",fd);
        }
        write(fd,buf,strlen(buf));
        printf("%d\n",n_write);
        close(fd);
        return 0;
}
           

4.文件光标移动操作(lseek巧妙计算文件大小)

函数原型

#include <sys/types.h>
#include <unistd.h>

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

/*DESCRIPTION
       lseek()  repositions the file offset of the open file description asso‐
       ciated with the file descriptor fd to the argument offset according  to
       the directive whence as follows:

       SEEK_SET
              The file offset is set to offset bytes.

       SEEK_CUR
              The  file  offset  is  set  to  its current location plus offset
              bytes.

       SEEK_END
              The file offset is set to the  size  of  the  file  plus  offset
              bytes.
*/
           

代码示例:

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

int main()
{
        int fd;
        int n_write;
        int n_read;
        char *buf = "Practice and review";
        char *readBuf = NULL;
        fd = open("./file1",O_RDWR|O_CREAT,0600);
        if(fd > 0){
                printf("open file1 success fd = %d\n",fd);
        }
        n_write = write(fd,buf,strlen(buf));
        printf("n_write = :%d\n",n_write);
        //lseek(fd,0,SEEK_SET);
        //lseek(fd,-19,SEEK_CUR);
        lseek(fd,-19,SEEK_END);
        readBuf = (char *)malloc(sizeof(char)*n_write + 1);
        n_read = read(fd,readBuf,n_write);
        printf("read %d byte,contex :%s\n",n_read,readBuf);
        close(fd);
        return 0;
}
           

lseek计算文件大小代码示例:

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

int main()
{
        int fd;
        int fileSize;
        fd = open("./file1",O_RDWR);
        fileSize = lseek(fd,0,SEEK_END);
        printf("file1 size = %d\n",fileSize);
        close(fd);
        return 0;
}