文件和路径名称获取

1. getcwd 获取当前工作目录:

  UNIX C函数。

相关函数:get_current_dir_name, getwd, chdir

头文件:#include <unistd.h>

定义函数:char * getcwd(char * buf, size_t size);

函数说明:getcwd()会将当前的工作目录绝对路径复制到参数buf 所指的内存空间,参数size 为buf 的空间大小。

注:
1、在调用此函数时,buf 所指的内存空间要足够大。若工作目录绝对路径的字符串长度超过参数size 大小,则返回NULL,errno 的值则为ERANGE。
2、倘若参数buf 为NULL,getcwd()会依参数size 的大小自动配置内存(使用malloc()),如果参数size 也为0,则getcwd()会依工作目录绝对路径的字符串程度来决定所配置的内存大小,进程可以在使用完次字符串后利用free()来释放此空间。


返回值:执行成功则将结果复制到参数buf 所指的内存空间, 或是返回自动配置的字符串指针. 失败返回NULL,错误代码存于errno.

范例
#include <unistd.h>
main()
{
    char buf[80];
    getcwd(buf, sizeof(buf));
    printf("current working directory : %s\n", buf);
}

执行:
current working directory :/tmp

2.stat()

功 能: 得到文件的信息,将其保存在buf结构中,buf的地址以参数形式传递给stat。
参数:
const char *path: 文件名或者目录名
struct _stat *buffer:结构体对象地址
返回值: 返回-1表示失败。
 
stat结构体定义:
stat 结构定义于:/usr/include/sys/stat.h 文件中
struct stat finfo;
stat( sFileName, &finfo );
int size = finfo. st_size;
struct stat
{
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //i-node节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态(属性)改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //文件内容对应的块数量
};
 
3.判断文件类型的宏:

在sys/stat.h中还定义了更多相关的函数

The following macros shall be provided to test whether a file is of the specified type. The value m supplied to the macros is the value of st_mode from a stat structure. The macro shall evaluate to a non-zero value if the test is true; 0 if the test is false.

S_ISBLK(m):Test for a block special file.
S_ISCHR(m):Test for a character special file.
S_ISDIR(m):Test for a directory.
S_ISFIFO(m):Test for a pipe or FIFO special file.
S_ISREG(m):Test for a regular file.
S_ISLNK(m):Test for a symbolic link.
S_ISSOCK(m):Test for a socket.

4.文件类型:

File type:

S_IFMT:Type of file.
S_IFBLK:Block special.
S_IFCHR:Character special.
S_IFIFO:FIFO special.
S_IFREG:Regular.
S_IFDIR:Directory.
S_IFLNK:Symbolic link.
S_IFSOCK:Socket. 
File mode bits:

S_IRWXU:Read, write, execute/search by owner.
S_IRUSR:Read permission, owner.
S_IWUSR:Write permission, owner.
S_IXUSR:Execute/search permission, owner.
S_IRWXG:Read, write, execute/search by group.
S_IRGRP:Read permission, group.
S_IWGRP:Write permission, group.
S_IXGRP:Execute/search permission, group.
S_IRWXO:Read, write, execute/search by others.
S_IROTH:Read permission, others.
S_IWOTH:Write permission, others.
S_IXOTH:Execute/search permission, others.
S_ISUID:Set-user-ID on execution.
S_ISGID:Set-group-ID on execution.
S_ISVTX:[XSI]  On directories, restricted deletion flag. 

The bits defined by S_IRUSR, S_IWUSR, S_IXUSR, S_IRGRP, S_IWGRP, S_IXGRP, S_IROTH, S_IWOTH, S_IXOTH, S_ISUID, S_ISGID, [XSI]   and S_ISVTX   shall be unique.

S_IRWXU is the bitwise-inclusive OR of S_IRUSR, S_IWUSR, and S_IXUSR.

S_IRWXG is the bitwise-inclusive OR of S_IRGRP, S_IWGRP, and S_IXGRP.

S_IRWXO is the bitwise-inclusive OR of S_IROTH, S_IWOTH, and S_IXOTH.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值