---
title: "linux下文件操作函数小结(1)"
author: "Linpro"
date: "2011-07-24T13:29:22Z"
tags: []
categories: ["非技术"]
slug: "linux-1"
draft: false
lastmod: "2011-07-24T13:30:27Z"
id: 1131605
url: "https://linpro.iteye.com/blog/1131605"
---

一、不带缓存的文件I/O操作函数
----------------------------------------------------------------------------------
1.creat

Fuction:create a file.

Header file:
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <fcntl.h>

Prototype:
	int create(const char *pathname,mode_t mode);

Parameter:
	pathname:the path of file.
	mode:   S_I(R/W/X)(USER/GRP/OTH)
		S_IRUSR:owner read right.
		S_IWUSR:owner write right.
		S_IXUSR:owner execute right.

		S_IRGRP:group member ~
		S_IWGRP:
		S_IXGRP:

		S_IROTH:others ~
		S_IWOTH:
		S_IXOTH:

Return value:
	success:return 0
	failed:return -1
----------------------------------------------------------------------------------
2.open

Function:open a exist file or create a new file.

Header file:
	#include <sys/types.h>
	#include <sys/stat.h>
	#include <fcntl.h>

Prototype:
	int open(const char *pathname,int flags);
	int open(const char *pathname,int flags,mode_t mode);

Parameter:
	pathname:the path of file.
	flags:
		O_RDONLY:read only mode.
		O_WRONLY:write mode.
		O_RDWR:read and write mode.
		O_APPEND:add to file end mode.
		O_TRUNG:clean file to zero.
		O_CREAT:create file.
		O_EXCL:together with O_CREATE,if file exist,error.

Return value:
	success:return a integer(file descriptor).
	failed:return -1.
----------------------------------------------------------------------------------
3.close

Fuction:close the file.

Header file:
	#include <unistd.h>

Prototype:
	int close(int fd);

Parameter:
	fd:file descriptor.

Return value:
	success: return 0
	failed:return -1
----------------------------------------------------------------------------------
4.read

Fuction:read the file.

Header file:#include <unistd.h>

Prototype:
	ssize_t read(int fd,void *buf,size_t count);

Parameter:
	fd:file descriptor.
	buf:a point to buffer.
	count:bytes of content.

Return value:
	success:return the number of bytes read.
	failed:return -1.
----------------------------------------------------------------------------------
5.write

Fuction:write to a file descriptor.

Header file:#include <unistd.h>

Prototype:
	ssize_t write(int fd, const void *buf, size_t count);

Parameter:
	fd:file descriptor.
	buf:a point to buffer.
	count:the max number of bytes write in.

Return value:
	success:the number of bytes written.
	failed:-1.
----------------------------------------------------------------------------------