C 语言文件和进程操作函数:全面的指南
C 语言文件和进程操作函数:全面的指南
C 语言提供了一系列函数,用于处理文件和进程,使程序能够与操作系统进行交互。本文将深入介绍这些关键函数,并提供代码示例,帮助您更好地理解和使用它们。
文件操作函数
-
access() - 检查文件是否存在及有无权限
#include <unistd.h> int access(const char *pathname, int mode);pathname: 要检查的文件路径mode: 权限模式(例如:R_OK表示可读,W_OK表示可写,X_OK表示可执行)
-
chmod() - 改变文件权限
#include <sys/stat.h> int chmod(const char *pathname, mode_t mode);pathname: 要改变权限的文件路径mode: 新的权限模式
-
chdir() - 切换当前工作目录
#include <unistd.h> int chdir(const char *path);path: 要切换到的目录路径
-
chroot() - 改变根目录
#include <unistd.h> int chroot(const char *path);path: 新的根目录路径
-
close() - 关闭文件
#include <unistd.h> int close(int fd);fd: 文件描述符
-
creat() - 创建文件
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int creat(const char *pathname, mode_t mode);pathname: 要创建的文件路径mode: 文件权限模式
-
dup() - 复制文件描述符
#include <unistd.h> int dup(int oldfd);oldfd: 要复制的文件描述符
-
dup2() - 复制文件描述符
#include <unistd.h> int dup2(int oldfd, int newfd);oldfd: 要复制的文件描述符newfd: 新的文件描述符
-
execve() - 执行程序
#include <unistd.h> int execve(const char *pathname, char *const argv[], char *const envp[]);pathname: 可执行程序的路径argv: 命令行参数数组envp: 环境变量数组
-
exit() - 退出当前进程
#include <stdlib.h>
void exit(int status);
status: 退出状态码
- fchmod() - 改变文件权限
#include <sys/stat.h>
int fchmod(int fd, mode_t mode);
fd: 文件描述符mode: 新的权限模式
- fchown() - 改变文件所有者
#include <sys/stat.h>
int fchown(int fd, uid_t owner, gid_t group);
fd: 文件描述符owner: 新的所有者用户IDgroup: 新的所有者组ID
- fcntl() - 文件控制操作
#include <fcntl.h>
int fcntl(int fd, int cmd, ...);
fd: 文件描述符cmd: 操作命令
- fork() - 创建子进程
#include <unistd.h>
pid_t fork(void);
- 返回值:在父进程中返回子进程的进程ID,在子进程中返回 0
- fstat() - 获取文件状态信息
#include <sys/stat.h>
int fstat(int fd, struct stat *buf);
fd: 文件描述符buf: 用于存储文件状态信息的结构体
- fsync() - 同步文件到磁盘
#include <unistd.h>
int fsync(int fd);
fd: 文件描述符
- ftruncate() - 截断文件
#include <unistd.h>
int ftruncate(int fd, off_t length);
fd: 文件描述符length: 新的文件长度
- getcwd() - 获取当前工作目录
#include <unistd.h>
char *getcwd(char *buf, size_t size);
buf: 用于存储目录路径的缓冲区size: 缓冲区大小
- getegid() - 获取有效组ID
#include <unistd.h>
gid_t getegid(void);
- geteuid() - 获取有效用户ID
#include <unistd.h>
uid_t geteuid(void);
- getgid() - 获取组ID
#include <unistd.h>
gid_t getgid(void);
- getgroups() - 获取组列表
#include <unistd.h>
int getgroups(int size, gid_t list[]);
size: 组列表的大小list: 用于存储组ID的数组
- getpgid() - 获取进程组ID
#include <unistd.h>
pid_t getpgid(pid_t pid);
pid: 进程ID
- getpid() - 获取进程ID
#include <unistd.h>
pid_t getpid(void);
- getppid() - 获取父进程ID
#include <unistd.h>
pid_t getppid(void);
- getuid() - 获取用户ID
#include <unistd.h>
uid_t getuid(void);
- kill() - 发送信号
#include <unistd.h>
int kill(pid_t pid, int sig);
pid: 要发送信号的进程IDsig: 信号编号
- link() - 创建硬链接
#include <unistd.h>
int link(const char *oldpath, const char *newpath);
oldpath: 原文件路径newpath: 新的硬链接路径
- lseek() - 移动文件指针位置
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
fd: 文件描述符offset: 相对偏移量whence: 相对位置(SEEK_SET、SEEK_CUR、SEEK_END)
- mkdir() - 创建目录
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
pathname: 要创建的目录路径mode: 目录权限模式
- open() - 打开文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, ...);
pathname: 文件路径flags: 打开模式标志
- pipe() - 创建管道
#include <unistd.h>
int pipe(int pipefd[2]);
pipefd: 用于存储管道文件描述符的数组
- read() - 从文件读取数据
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
fd: 文件描述符buf: 用于存储读取数据的缓冲区count: 要读取的字节数
- rmdir() - 删除目录
#include <unistd.h>
int rmdir(const char *pathname);
pathname: 要删除的目录路径
- select() - 等待文件描述符准备就绪
#include <sys/select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
nfds: 文件描述符的最大值加 1readfds: 可读文件描述符集合writefds: 可写文件描述符集合exceptfds: 异常文件描述符集合timeout: 超时时间
- setgid() - 设置组ID
#include <unistd.h>
int setgid(gid_t gid);
gid: 要设置的组ID
- setpgid() - 设置进程组ID
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
pid: 进程IDpgid: 要设置的进程组ID
- setsid() - 创建新会话
#include <unistd.h>
pid_t setsid(void);
- 返回值:新会话的进程ID
- setuid() - 设置用户ID
#include <unistd.h>
int setuid(uid_t uid);
uid: 要设置的用户ID
- stat() - 获取文件状态信息
#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
pathname: 文件路径buf: 用于存储文件状态信息的结构体
- symlink() - 创建符号链接
#include <unistd.h>
int symlink(const char *target, const char *linkpath);
target: 目标文件路径linkpath: 符号链接路径
- sync() - 同步缓存到磁盘
#include <unistd.h>
void sync(void);
- tcgetattr() - 获取终端属性
#include <termios.h>
int tcgetattr(int fd, struct termios *termios_p);
fd: 终端文件描述符termios_p: 用于存储终端属性的结构体
- tcsetattr() - 设置终端属性
#include <termios.h>
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
fd: 终端文件描述符optional_actions: 设置操作termios_p: 包含终端属性的结构体
- unlink() - 删除文件
#include <unistd.h>
int unlink(const char *pathname);
pathname: 要删除的文件路径
- wait() - 等待子进程结束
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
status: 用于存储子进程退出状态的指针
- write() - 向文件写入数据
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
fd: 文件描述符buf: 包含要写入数据的缓冲区count: 要写入的字节数
总结
本文介绍了 C 语言中用于文件和进程操作的关键函数,包括文件访问、权限管理、进程创建、信号发送等。您可以根据需要使用这些函数来完成各种操作。
原文地址: https://www.cveoy.top/t/topic/m0Zy 著作权归作者所有。请勿转载和采集!