c语言中目录及文件操作

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1. 错误处理与错误号

cat /usr/include/asm-generic/errno-base.h

#define EPERM 1 /* Operation not per mitted */

#define ENOENT 2 /* No suc h file or director y */

#define ESRCH 3 /* No suc h proc ess */

#define EINTR 4 /* Interrupted s y stem call */

#define EIO 5 /* I/O error */

#define ENXIO 6 /* No such dev ice or address */

#define E2BIG 7 /* Argument list too long */

#define ENOEXEC 8 /* Ex ec format error */

#define EBADF 9 /* Bad file number */

#define ECHILD 10 /* No c hild process es */

#define EAGAIN 11 /* Try again */

#define ENOMEM 12 /* Out of memor y */

#define EACCES 13 /* Per mission denied */

#define EFAULT 14 /* Bad address */

#define ENOTBLK 15 /* Bl oc k dev ice required */

#define EBUSY 16 /* Dev ice or resource bus y */

#define EEXIST 17 /* File ex ists */

#define EXDEV 18 /* Cross-dev ice link */

#define ENODEV 19 /* N o suc h dev ice */

#define ENOTDIR 20 /* Not a direc tor y */

#define EISDIR 21 /* Is a director y */

#define EINVAL 22 /* Inv alid argument */

#define ENFILE 23 /* File table ov erflow */

#define EMFILE 24 /* Too many open files */

#define ENOTTY 25 /* Not a ty pewriter */

#define ETXTBSY 26 /* Text file bus y */

#define EFBIG 27 /* File too large */

#define ENOSPC 28 /* N o space l eft on dev ic e */

#define ESPIPE 29 /* Illegal seek */

#define EROFS 30 /* Read-onl y file s y s tem */

#define EMLINK 31 /* T oo many link s */

#define EPIPE 32 /* Brok en pipe */

#define EDOM 33 /* Math argument out of domain of func */ #define ERANGE 34 /* M ath r esult not repres entabl e */

1.1 用错误常数显示错误信息

函数strerror()可以把一个错误常数转换成一个错误提示语句。char *strerror(int errnum);

#include

#include

#include

int main()

{

int i;

for(i=1;i<15;i++)

{

printf("Errno:%d ",i);

printf("Message:%s/n",s trerror(i));

}

}

2. 创建与删除目录

2.1 创建目录函数mk dir()

int mkdir(char *pathname,mode_t mode);

pathname //是一个字符型指针,表示需要创建的目录路径。mode //表示权限的八进制数字

作用:创建目录

返回值:创建成功返回整数0,否则返回-1

头文件:“sy s/types.h”“sy s/s tat.h”

可能返回的错误常数:

EPERM:目录中有不合规则的名字

EEXIST:参数pathname所指的目录已存在

EFAULT:pathname指向了非法的地址

EACCESS:权限不足,不允许创建目录ENAMETOOLONG:参数pathname太长

ENOENT:所指的上级目录不存在

ENOTDIR:参数pathname不是目录

ENOMEM:核心内存不足

EROFS:欲创建的目录在只读文件系统内

ELOOP:参数pathname有多个符合的链接

ENOSPC:磁盘空间不足

#include

#include

#include

#include

int main()

{

ex tern int errno;

char *path="/root/tmp11";

if(mk dir(path,0766)==0)

{

printf("created the direc tor y %s./n",path);

}

else

{

printf("cann't create the director y %s/n",path);

printf("errno:%d/n",errno);