Linux下的文件操作(一)

一、文件描述符

文件描述符是一個非負整數,表示一個打開的文件,內核中有三個特殊的文件描述符,“stdin”,“stdout”,“stderr”,他們所對應的值爲0,1,2,他們能的宏定義在unistd.h頭文件當中。
由於記錄文件相關信息的結構體記錄在內核當中,爲了不暴露內核的地址,因此文件結構體指針不能直接給用戶直接使用。而內核中會存在一張表記錄文件描述符和他對應文件指針,因此文件描述符就相當於是文件指針的下標。

二、creat/open/close 函數

1、頭文件

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

2、creat函數

int creat(const char *pathname, mode_t mode);
//功能 :創建一個文件
//參數1:創建文件的路徑
//參數2:創建文件的權限
//mode:設置文件的存儲權限
		//USR用戶的權限 X:可執行,R:讀權限,W寫權限
		//S_IRWXU  00700 read,write and execute permission
		//S_IRUSR  00400 user has read permission
		//S_IWUSR  00200 user has write permission
		//S_IXUSR  00100 user has execute permission
		
		//GRP組的權限 X:可執行,R:讀權限,W寫權限
        //S_IRWXG  00070 read,write and execute permission
		//S_IRGRP  00040 group has read permission
        //S_IWGRP  00020 group has write permission
		//S_IXGRP  00010 group has execute permission
		
		//OTH其他用戶的權限 X:可執行,R:讀權限,W寫權限
		//S_IRWXO  00007 others have read, write and
		//S_IROTH  00004 others have read permission
		//S_IWOTH  00002 others have write permission
		//S_IXOTH  00001 others have execute  permis‐sion
//返回值:成功返回文件描述符,失敗返回-1

使用方法:

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

int main()
{
	int fd = creat("./test.txt",0644);
	printf("%d\n",fd);
}
//在當前路徑下創建一個test.txt文件,用戶具有讀寫權限,組和其他用戶只有讀權限

3、open函數

int open(const char *pathname, int flags);
//功能:打開一個文件
//參數1:文件路徑
//參數2:打開文件的方式
		//O_WRONLY	以只寫的方式打開
		//O_RDONLY	以只讀的方式打開
		//O_RDWR	以讀寫的方式打開
		//O_APPEND	寫的方式打開後追加到文件末尾
		//O_TRUNC	寫的方式打開,清空文件數據
		//O_CREATE	創建一個文件與create()函數功能大致相同
		//O_EXCL	如果文件存在則失敗通常配合O_CREATE使用
 int open(const char *pathname, int flags, mode_t mode);
 //多了一個mode參數表示創建文件的權限,當參數2爲O_CREATE必須要加mode參數。

使用方法

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main()
{
	int fd1 = open("./open.txt",O_CREAT|O_EXCL|O_RDWR,0644);//創建一個open.txt文件
	//int fd2 = open("./open.txt",O_RDWR); 以讀寫的方式打開一個文件
	printf("%d",fd);
}     

4、close函數

int close(int fd);
//功能:關閉文件
//參數:文件描述符
//返回值:關閉成功返回0,否則返回-1

三、read\write函數

1、write函數

 ssize_t write(int fd, const void *buf, size_t count);
 //功能將數據寫入到文件中
 //參數1:要寫入的文件描述符
 //參數2:要寫入的數據地址
 //參數3:要寫入的字節數
 //返回值:成功寫入的字節數

使用方法:

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

int main()
{
	int fd = open("./write.txt",O_WRONLY|O_CREAT,0644);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}
	
	char buf[1024] = "hello world";
	int ret = write(fd,buf,strlen(buf));
	printf("ret=%d\n",ret);
}

2、read函數

 ssize_t read(int fd, void *buf, size_t count);
 //功能從文件中讀取數據
 //參數1:要讀取的文件描述符
 //參數2:讀取的數據存儲到buf
 //參數3:讀取的字節數
 //返回值:成功讀取到字節數

使用方法

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

int main()
{
	int fd = open("./write.txt",O_RDONLY);
	if(fd < 0)
	{
		perror("open");
		return -1;
	}

	char buf[1024] = {};
	read(fd,buf,sizeof(buf));
	puts(buf);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章