C 语言文件和进程操作函数:全面的指南

C 语言提供了一系列函数,用于处理文件和进程,使程序能够与操作系统进行交互。本文将深入介绍这些关键函数,并提供代码示例,帮助您更好地理解和使用它们。

文件操作函数

  1. access() - 检查文件是否存在及有无权限

    #include <unistd.h>
    int access(const char *pathname, int mode);
    
    • pathname: 要检查的文件路径
    • mode: 权限模式(例如:R_OK 表示可读,W_OK 表示可写,X_OK 表示可执行)
  2. chmod() - 改变文件权限

    #include <sys/stat.h>
    int chmod(const char *pathname, mode_t mode);
    
    • pathname: 要改变权限的文件路径
    • mode: 新的权限模式
  3. chdir() - 切换当前工作目录

    #include <unistd.h>
    int chdir(const char *path);
    
    • path: 要切换到的目录路径
  4. chroot() - 改变根目录

    #include <unistd.h>
    int chroot(const char *path);
    
    • path: 新的根目录路径
  5. close() - 关闭文件

    #include <unistd.h>
    int close(int fd);
    
    • fd: 文件描述符
  6. creat() - 创建文件

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    int creat(const char *pathname, mode_t mode);
    
    • pathname: 要创建的文件路径
    • mode: 文件权限模式
  7. dup() - 复制文件描述符

    #include <unistd.h>
    int dup(int oldfd);
    
    • oldfd: 要复制的文件描述符
  8. dup2() - 复制文件描述符

    #include <unistd.h>
    int dup2(int oldfd, int newfd);
    
    • oldfd: 要复制的文件描述符
    • newfd: 新的文件描述符
  9. execve() - 执行程序

    #include <unistd.h>
    int execve(const char *pathname, char *const argv[], char *const envp[]);
    
    • pathname: 可执行程序的路径
    • argv: 命令行参数数组
    • envp: 环境变量数组
  10. exit() - 退出当前进程

#include <stdlib.h>
void exit(int status);
  • status: 退出状态码
  1. fchmod() - 改变文件权限
#include <sys/stat.h>
int fchmod(int fd, mode_t mode);
  • fd: 文件描述符
  • mode: 新的权限模式
  1. fchown() - 改变文件所有者
#include <sys/stat.h>
int fchown(int fd, uid_t owner, gid_t group);
  • fd: 文件描述符
  • owner: 新的所有者用户ID
  • group: 新的所有者组ID
  1. fcntl() - 文件控制操作
#include <fcntl.h>
int fcntl(int fd, int cmd, ...);
  • fd: 文件描述符
  • cmd: 操作命令
  1. fork() - 创建子进程
#include <unistd.h>
pid_t fork(void);
  • 返回值:在父进程中返回子进程的进程ID,在子进程中返回 0
  1. fstat() - 获取文件状态信息
#include <sys/stat.h>
int fstat(int fd, struct stat *buf);
  • fd: 文件描述符
  • buf: 用于存储文件状态信息的结构体
  1. fsync() - 同步文件到磁盘
#include <unistd.h>
int fsync(int fd);
  • fd: 文件描述符
  1. ftruncate() - 截断文件
#include <unistd.h>
int ftruncate(int fd, off_t length);
  • fd: 文件描述符
  • length: 新的文件长度
  1. getcwd() - 获取当前工作目录
#include <unistd.h>
char *getcwd(char *buf, size_t size);
  • buf: 用于存储目录路径的缓冲区
  • size: 缓冲区大小
  1. getegid() - 获取有效组ID
#include <unistd.h>
gid_t getegid(void);
  1. geteuid() - 获取有效用户ID
#include <unistd.h>
uid_t geteuid(void);
  1. getgid() - 获取组ID
#include <unistd.h>
gid_t getgid(void);
  1. getgroups() - 获取组列表
#include <unistd.h>
int getgroups(int size, gid_t list[]);
  • size: 组列表的大小
  • list: 用于存储组ID的数组
  1. getpgid() - 获取进程组ID
#include <unistd.h>
pid_t getpgid(pid_t pid);
  • pid: 进程ID
  1. getpid() - 获取进程ID
#include <unistd.h>
pid_t getpid(void);
  1. getppid() - 获取父进程ID
#include <unistd.h>
pid_t getppid(void);
  1. getuid() - 获取用户ID
#include <unistd.h>
uid_t getuid(void);
  1. kill() - 发送信号
#include <unistd.h>
int kill(pid_t pid, int sig);
  • pid: 要发送信号的进程ID
  • sig: 信号编号
  1. link() - 创建硬链接
#include <unistd.h>
int link(const char *oldpath, const char *newpath);
  • oldpath: 原文件路径
  • newpath: 新的硬链接路径
  1. lseek() - 移动文件指针位置
#include <unistd.h>
off_t lseek(int fd, off_t offset, int whence);
  • fd: 文件描述符
  • offset: 相对偏移量
  • whence: 相对位置(SEEK_SETSEEK_CURSEEK_END
  1. mkdir() - 创建目录
#include <sys/stat.h>
int mkdir(const char *pathname, mode_t mode);
  • pathname: 要创建的目录路径
  • mode: 目录权限模式
  1. open() - 打开文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags, ...);
  • pathname: 文件路径
  • flags: 打开模式标志
  1. pipe() - 创建管道
#include <unistd.h>
int pipe(int pipefd[2]);
  • pipefd: 用于存储管道文件描述符的数组
  1. read() - 从文件读取数据
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
  • fd: 文件描述符
  • buf: 用于存储读取数据的缓冲区
  • count: 要读取的字节数
  1. rmdir() - 删除目录
#include <unistd.h>
int rmdir(const char *pathname);
  • pathname: 要删除的目录路径
  1. select() - 等待文件描述符准备就绪
#include <sys/select.h>
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
  • nfds: 文件描述符的最大值加 1
  • readfds: 可读文件描述符集合
  • writefds: 可写文件描述符集合
  • exceptfds: 异常文件描述符集合
  • timeout: 超时时间
  1. setgid() - 设置组ID
#include <unistd.h>
int setgid(gid_t gid);
  • gid: 要设置的组ID
  1. setpgid() - 设置进程组ID
#include <unistd.h>
int setpgid(pid_t pid, pid_t pgid);
  • pid: 进程ID
  • pgid: 要设置的进程组ID
  1. setsid() - 创建新会话
#include <unistd.h>
pid_t setsid(void);
  • 返回值:新会话的进程ID
  1. setuid() - 设置用户ID
#include <unistd.h>
int setuid(uid_t uid);
  • uid: 要设置的用户ID
  1. stat() - 获取文件状态信息
#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
  • pathname: 文件路径
  • buf: 用于存储文件状态信息的结构体
  1. symlink() - 创建符号链接
#include <unistd.h>
int symlink(const char *target, const char *linkpath);
  • target: 目标文件路径
  • linkpath: 符号链接路径
  1. sync() - 同步缓存到磁盘
#include <unistd.h>
void sync(void);
  1. tcgetattr() - 获取终端属性
#include <termios.h>
int tcgetattr(int fd, struct termios *termios_p);
  • fd: 终端文件描述符
  • termios_p: 用于存储终端属性的结构体
  1. tcsetattr() - 设置终端属性
#include <termios.h>
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);
  • fd: 终端文件描述符
  • optional_actions: 设置操作
  • termios_p: 包含终端属性的结构体
  1. unlink() - 删除文件
#include <unistd.h>
int unlink(const char *pathname);
  • pathname: 要删除的文件路径
  1. wait() - 等待子进程结束
#include <sys/types.h>
#include <sys/wait.h>
pid_t wait(int *status);
  • status: 用于存储子进程退出状态的指针
  1. write() - 向文件写入数据
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
  • fd: 文件描述符
  • buf: 包含要写入数据的缓冲区
  • count: 要写入的字节数

总结

本文介绍了 C 语言中用于文件和进程操作的关键函数,包括文件访问、权限管理、进程创建、信号发送等。您可以根据需要使用这些函数来完成各种操作。

C 语言文件和进程操作函数:全面的指南

原文地址: https://www.cveoy.top/t/topic/m0Zy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录