查询网站域名,上海平台网站建设哪家好,弄一个公司网站需要多少钱,贞丰县住房和城乡建设局网站一、目录相关函数及属性
1、 opendir()函数 opendir 用于打开一个目录#xff0c;并返回一个目录流指针。name#xff1a;要打开的目录的路径。 返回值#xff1a;成功时返回指向目录流的指针#xff0c;失败时返回 NULL。
#include dirent.h
DIR *opendir(con…一、目录相关函数及属性
1、 opendir()函数 opendir 用于打开一个目录并返回一个目录流指针。name要打开的目录的路径。 返回值成功时返回指向目录流的指针失败时返回 NULL。
#include dirent.h
DIR *opendir(const char *name);2、readdir()函数 readdir用于读取目录中的一个条目如文件或子目录。每次调用会返回下一个目录条目直到所有条目都读取完毕. dirp通过opendir打开的目录流指针。 返回值成功时返回指向目录条目的指针失败时返回 NULL。
#include dirent.h
struct dirent *readdir(DIR *dirp);struct dirent结构通常包含以下成员d_name文件或目录的名称。d_ino文件的索引节点号inode number。
3、closedir()函数 closedir用于关闭一个已打开的目录流。 参数opendir的返回结果中目录流对象。返回值成功时返回 0失败时返回 -1
#include dirent.h
int closedir(DIR *dirp);4、chdir函数 chdir用于更改当前工作目录。参数: path:改变到的路径。返回值:成功返回0、失败返回-1。
#include unistd.h
int chdir(const char *path);
5、getcwd函数pwd getcwd用于获取当前工作目录的绝对路径参数: buf:保存工作路径空间的首地址、 size:保存路径空间的长度 返回值: 成功返回包含路径空间的字符串首地址、失败返回NULL。
#include unistd.h
char *getcwd(char *buf, size_t size);6、mkdir函数mkdir mkdir()用于创建一个新的目录、LInux中区分大小写的。 pathname要创建的目录的路径。 mode目录的权限设置如 0755 表示所有者可以读写执行组和其他用户只读执行。 返回值成功时返回 0失败时返回 -1。
#include sys/stat.h
#include sys/types.h
int mkdir(const char *pathname, mode_t mode);7、rmdir函数rm rmdir()删除一个空目录文件 pathname要删除的目录的路径。该目录必须是空的否则删除会失败。 返回值成功时返回 0失败时返回 -1
#include unistd.h
int rmdir(const char *pathname);8、remove函数mv remove用于删除指定的文件或空目录。 pathname要删除的文件或空目录的路径。 返回值成功时返回 0失败时返回 -1。 说明 如果路径指向的是一个文件remove 将删除该文件。 如果路径指向的是一个目录则该目录必须为空否则删除将失败。 删除非空目录remove不能删除非空目录如果需要删除非空目录应使用系统特定的函数或递归删除目录下的所有内容。
#include stdio.h
int remove(const char *pathname);9、rename函数mv rename用于重命名文件或目录或者将文件或目录移动到新的位置。 oldpath原文件或目录的路径。 newpath新文件或目录的路径可以是新的名字或新的路径。 返回值成功时返回 0失败时返回 -1。
说明 如果newpath已经存在并且newpath 是一个文件那么它将被删除并替换为oldpath。 如果newpath是一个目录且目录非空那么重命名操作将失败。 rename既可以重命名文件也可以重命名目录甚至可以跨目录移动文件或目录。 跨文件系统重命名在某些文件系统上rename可能不支持跨文件系统重命名或移动文件在这种情况下通常需要手动复制并删除文件
#include stdio.h
int rename(const char *oldpath, const char *newpath);10、stat()函数stat stat是一个在类 Unix 操作系统中非常有用的系统调用用于获取文件或目录的详细信息包括文件的大小、权限、修改时间等。使用stat可以获取一个文件或目录的状态信息。 pathname文件或目录的路径。 statbuf指向 struct stat结构体的指针用于存储获取的文件或目录的状态信息。 返回值成功时返回 0失败时返回 -1。
#include sys/types.h
#include sys/stat.h
#include unistd.h
int stat(const char *pathname, struct stat *statbuf);stat函数通过struct stat 结构返回文件的状态信息。该结构体定义如下
struct stat {dev_t st_dev; // 文件所在的设备ino_t st_ino; // 文件的节点号mode_t st_mode; // 文件的类型和权限nlink_t st_nlink; // 硬链接的数量uid_t st_uid; // 所有者的用户IDgid_t st_gid; // 所有者的组IDdev_t st_rdev; // 若此文件为设备文件则为其设备号off_t st_size; // 文件大小以字节为单位blksize_t st_blksize; // 文件系统的块大小blkcnt_t st_blocks; // 文件所占的块数time_t st_atime; // 最后一次访问时间time_t st_mtime; // 最后一次修改时间time_t st_ctime; // 最后一次状态变化时间
};常用的 st_mode 值 S_IFREG普通文件 S_IFDIR目录 S_IFLNK符号链接 S_IFCHR字符设备 S_IFBLK块设备 S_IFIFOFIFO (管道) S_IFSOCK套接字
此外st_mode中还包含了文件权限位如 S_IRUSR所有者可读 S_IWUSR所有者可写 S_IXUSR所有者可执行
11、truncate函数 truncate是一个系统调用用于调整文件的大小。如果新大小小于文件当前大小则文件会被截断如果新大小大于文件当前大小则文件会被扩展并用空字节通常为零填充扩展部分。 path文件的路径。 length文件的新大小。 返回值成功时返回 0失败时返回 -1
二、软链接与硬链接 软链接符号链接是一个指向另一个文件或目录的特殊文件。它类似于 Windows 中的快捷方式。//ln -s 123 softlink 快捷方式
特点
软链接只是一个指向目标文件的路径并不包含目标文件的内容。软链接可以跨文件系统创建。如果目标文件被删除软链接会变为“断开的链接”因为它指向一个不存在的文件。 target目标文件或目录的路径名。 linkpath符号链接的路径名即将要创建的符号链接的名称。 返回值成功时返回 0失败时返回 -1
#include unistd.h
int symlink(const char *target, const char *linkpath);硬链接是文件系统中指向同一文件数据的多个文件名。硬链接与目标文件共享相同的 inode 号码因此它们实际上是同一个文件只是有不同的文件名。//link ln 1 2
特点
硬链接指向相同的文件数据删除一个硬链接不会影响其他硬链接。硬链接不能跨文件系统创建。硬链接不能链接到目录除非使用特定的系统选项或特殊的文件系统 参数 oldpath现有文件的路径名。 newpath硬链接的路径名即将要创建的硬链接的名称。 返回值成功时返回 0失败时返回 -1。
#include unistd.h
int link(const char *oldpath, const char *newpath);三、报错函数 在 C 语言中perror()、strerror()和 error()是处理和显示错误信息的常用函数或宏。它们主要用于调试和错误处理让程序员可以理解程序运行时的错误原因。 1.perror() 函数 void perror(const char *s); perror()是一个标准库函数(stdio.h库)用于输出上一个库函数调用失败的原因。它会根据全局变量 errno的值来输出相应的错误信息。 s这是一个字符串通常是描述发生错误的上下文。perror()会先输出这个字符串然后在后面加上一个冒号和空格再输出根据 errno 解释的错误信息。无返回值
#include stdio.h
int main() {FILE *fp fopen(non_existent_file.txt, r);if (fp NULL) {perror(Failed to open file);return -1;}return 0;
}
//输出Failed to open file: No such file or directory2.strerror()函数 char *strerror(int errnum); strerror()是一个标准库函数(string.h库)用于将错误号通常是 errno转换为描述错误的字符串。它返回一个指向错误消息字符串的指针。 errnum这是一个错误号通常是 errno的值。 返回值返回指向错误消息字符串的指针
#include stdio.h
#include string.h
#include errno.h//错误号变量int main() {int errnum ENOENT; // No such file or directory error codeprintf(Error message: %s\n, strerror(errnum));//输出Error message: No such file or directoryreturn 0;
}3.error()函数 error()不是标准的 C 库函数但在 GNU C 库 (glibc) 中可用。它用于打印错误消息并可选地终止程序。error() 函数还允许指定一个自定义的退出状态码。 status如果 status不为 0error()会调用 exit(status)以终止程序如果为 0程序将继续执行。 errnum通常是 errno表示错误号。如果 errnum为 0不会输出任何错误消息只输出 format指定的自定义信息。 format这是一个格式化字符串类似于 printf用于输出自定义错误消息。 返回值无返回值
#include stdio.h
#include error.h
#include errno.h //extern int errno
int main(int argc, char *argv[])
{FILE* fp fopen(aaa,r); if(NULL fp){//perror(fopen);error(1,errno,fopen,file:%s fun:%s line:%d,__FILE__,__func__,__LINE__);printf(aaaa\n);return -1;}return 0;
}
总结 perror()输出自定义字符串和标准错误消息适用于快速输出错误信息。 strerror()将错误码转换为可读的错误字符串适用于需要将错误信息作为字符串处理的场合。 error()GNU扩展输出格式化的错误消息并可选择是否退出程序提供了更灵活的错误处理方式。 __FILE__: 表示是那个文件 __LINE__:表示第几行 __func__:表示在那个函数 __DATE__:编译时表示日期 __TIME__:编译时表示时间
四、获取用户和组相关函数
1.组函数
struct group {char *gr_name; // 组名char *gr_passwd; // 组密码gid_t gr_gid; // 组IDchar **gr_mem; // 组成员列表
};1.getgrgid()函数 getgrgid()函数用于根据组ID (GID) 查找组信息并返回指向 struct group结构体的指针。 返回值指向 struct group结构体的指针包含了组的名称、组ID、组成员等信息。如果没有找到对应的组返回 NULL。
#include stdio.h
#include grp.h
int main() {gid_t gid 1000; // 例如组ID为1000//struct group *getgrgid(gid_t gid);struct group *grp getgrgid(gid);if (grp ! NULL) {printf(Group name: %s\n, grp-gr_name);} else {perror(Group not found);}return 0;
}2.getgrnam()函数 getgrnam()函数用于根据组名查找组信息并返回指向 struct group结构体的指针。 返回值指向 struct group 结构体的指针。如果没有找到对应的组返回 NULL
#include stdio.h
#include grp.h
//struct group *getgrnam(const char *name);
int main() {const char *group_name staff; // 例如组名为staffstruct group *grp getgrnam(group_name);if (grp ! NULL) {printf(Group ID: %d\n, grp-gr_gid);} else {perror(Group not found);}return 0;
}2.用户函数
struct passwd {char *pw_name; // 用户名char *pw_passwd; // 用户密码uid_t pw_uid; // 用户IDgid_t pw_gid; // 组IDchar *pw_gecos; // 用户全名char *pw_dir; // 用户的主目录char *pw_shell; // 用户的登录 shell
};getpwuid()函数 getpwuid()函数用于根据用户ID (UID) 查找用户信息并返回指向 struct passwd结构体的指针。
#include stdio.h
#include pwd.h
int main() {uid_t uid 1000; // 例如用户ID为1000struct passwd *pw getpwuid(uid);if (pw ! NULL) {printf(User name: %s\n, pw-pw_name);} else {perror(User not found);}return 0;
}getpwnam()函数 getpwnam()函数用于根据用户名查找用户信息并返回指向 struct passwd结构体的指针。
#include stdio.h
#include pwd.h
int main() {const char *user_name john; // 例如用户名为johnstruct passwd *pw getpwnam(user_name);if (pw ! NULL) {printf(User ID: %d\n, pw-pw_uid);} else {perror(User not found);}return 0;
}五、时间函数 C 语言中的时间相关函数主要包含在 time.h 头文件中。这些函数用于处理和操作日期和时间支持获取当前时间、格式化时间、测量时间间隔等操作。
struct tm {int tm_sec; // 秒范围为 0-60允许闰秒考虑到可能存在的闰秒所以有可能为 60int tm_min; // 分范围为 0-59int tm_hour; // 时范围为 0-23int tm_mday; // 一个月中的某一天范围为 1-31int tm_mon; // 月范围为 0-110 表示 1 月1 表示 2 月以此类推int tm_year; // 年份自 1900 年起算即 2024 年表示为 2024-1900 124int tm_wday; // 一周中的某一天范围为 0-60 表示星期天1 表示星期一以此类推int tm_yday; // 一年中的某一天范围为 0-3650 表示 1 月 1 日int tm_isdst; // 夏令时标志正值表示夏令时生效零表示夏令时不生效负值表示不确定
};以下是一些常用的时间相关函数
1. time()函数 time()函数用于获取当前的日历时间并返回Epoch时间1970-01-01 00:00:00 UTC以来经过的秒数。 t如果t不是NULL则time_t值也会存储在t指向的内存中。 返回值返回当前时间的time_t值通常是一个长整型整数如果失败返回(time_t)-1。
#include time.h
time_t time(time_t *t);2. ctime()函数 ctime()函数将时间值time_t类型转换为字符串形式的本地时间格式为 Www Mmm dd hh:mm:ss yyyy\n。 t指向要转换的time_t时间值的指针。 返回值返回指向字符串的指针包含格式化的本地时间字符串。如果失败返回 NULL。
#include stdio.h
#include time.hint main() {time_t current_time time(NULL);printf(Current time: %s, ctime(current_time));
//Current time: Tue Aug 15 14:30:00 2024return 0;
}3. difftime()函数 difftime()函数用于计算两个时间值之间的差值并以秒为单位返回结果。 end结束时间的time_t值。 start开始时间的time_t值。 返回值返回end和start之间的时间差以秒为单位。
#include stdio.h
#include time.hint main() {time_t start_time time(NULL);// 模拟某种延迟sleep(2);time_t end_time time(NULL);double elapsed difftime(end_time, start_time);printf(Elapsed time: %.2f seconds\n, elapsed);return 0;
}4. localtime()函数 localtime()函数将time_t类型的时间值转换为当地时间并返回指向struct tm结构体的指针。 timep指向要转换的time_t时间值的指针。 返回值返回指向 struct tm的指针包含分解的本地时间信息。如果失败返回 NULL。
#include stdio.h
#include time.hint main() {time_t current_time time(NULL);struct tm *local_time localtime(current_time);printf(Local time: %02d:%02d:%02d\n,local_time-tm_hour, local_time-tm_min, local_time-tm_sec);return 0;
}5. mktime()函数 mktime()函数将struct tm结构体表示的时间转换为time_t类型的日历时间秒数。 timeptr指向要转换的 struct tm结构体的指针。 返回值返回 time_t值自 Epoch 时间以来的秒数如果失败返回 (time_t)-1。
#include stdio.h
#include time.hint main() {struct tm time_info {0};time_info.tm_year 2024 - 1900; // 年份从1900年开始time_info.tm_mon 7; // 8月从0开始time_info.tm_mday 15;time_info.tm_hour 14;time_info.tm_min 30;time_info.tm_sec 0;time_t t mktime(time_info);printf(Time in seconds since Epoch: %ld\n, t);return 0;
}6. strftime()函数 strftime()函数根据格式字符串生成格式化的时间字符串。 s存放结果的字符串缓冲区。 max缓冲区的最大长度。 format格式字符串类似于printf的格式字符串。 tm指向struct tm结构体的指针包含时间信息。 返回值返回写入缓冲区的字符数。
#include stdio.h
#include time.hint main() {time_t current_time time(NULL);struct tm *local_time localtime(current_time);char buffer[80];strftime(buffer, sizeof(buffer), Date: %Y-%m-%d Time: %H:%M:%S, local_time);printf(%s\n, buffer);return 0;
}总结 time()获取当前时间以秒为单位。 ctime()将time_t时间转换为字符串形式的本地时间。 difftime()计算两个time_t时间之间的差值。 localtime()将time_t时间转换为本地时间的struct tm结构体。 gmtime()将time_t时间转换为 UTC 时间的struct tm结构体。 mktime()将 struct tm结构体转换为time_t时间。 strftime()根据格式字符串生成格式化的时间字符串。
这些函数提供了丰富的时间处理功能适用于不同的时间和日期操作场景。 文章转载自: http://www.morning.tqrxm.cn.gov.cn.tqrxm.cn http://www.morning.drkk.cn.gov.cn.drkk.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.dshxj.cn.gov.cn.dshxj.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.c7510.cn.gov.cn.c7510.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.rjjys.cn.gov.cn.rjjys.cn http://www.morning.rnxw.cn.gov.cn.rnxw.cn http://www.morning.dqrhz.cn.gov.cn.dqrhz.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.cmcjp.cn.gov.cn.cmcjp.cn http://www.morning.rlqqy.cn.gov.cn.rlqqy.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.tllhz.cn.gov.cn.tllhz.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.hsdhr.cn.gov.cn.hsdhr.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.smpmn.cn.gov.cn.smpmn.cn http://www.morning.zknxh.cn.gov.cn.zknxh.cn http://www.morning.slnz.cn.gov.cn.slnz.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.rkxdp.cn.gov.cn.rkxdp.cn http://www.morning.rmdwp.cn.gov.cn.rmdwp.cn http://www.morning.rjbb.cn.gov.cn.rjbb.cn http://www.morning.ftnhr.cn.gov.cn.ftnhr.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.zfkxj.cn.gov.cn.zfkxj.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.ykbgs.cn.gov.cn.ykbgs.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.bdwqy.cn.gov.cn.bdwqy.cn http://www.morning.wmgjq.cn.gov.cn.wmgjq.cn http://www.morning.wrqw.cn.gov.cn.wrqw.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.rblqk.cn.gov.cn.rblqk.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.rddlz.cn.gov.cn.rddlz.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.zpkfb.cn.gov.cn.zpkfb.cn http://www.morning.qcslh.cn.gov.cn.qcslh.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.kwblwbl.cn.gov.cn.kwblwbl.cn http://www.morning.jqsyp.cn.gov.cn.jqsyp.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.ppqzb.cn.gov.cn.ppqzb.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.gpryk.cn.gov.cn.gpryk.cn http://www.morning.mxnrl.cn.gov.cn.mxnrl.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.xcxj.cn.gov.cn.xcxj.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.kghss.cn.gov.cn.kghss.cn http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.jmbgl.cn.gov.cn.jmbgl.cn http://www.morning.thntp.cn.gov.cn.thntp.cn http://www.morning.sjwzl.cn.gov.cn.sjwzl.cn http://www.morning.gsyns.cn.gov.cn.gsyns.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.prqdr.cn.gov.cn.prqdr.cn http://www.morning.clgbb.cn.gov.cn.clgbb.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.rmkyb.cn.gov.cn.rmkyb.cn http://www.morning.xqtqm.cn.gov.cn.xqtqm.cn http://www.morning.dcdhj.cn.gov.cn.dcdhj.cn http://www.morning.ldhbs.cn.gov.cn.ldhbs.cn http://www.morning.rstrc.cn.gov.cn.rstrc.cn http://www.morning.ypxyl.cn.gov.cn.ypxyl.cn