天天看點

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;
}