- c语言学习计划
- C语言简介
- C语言基本语法
- C语言-控制结构
- C语言-输入输出
- C语言-数组和字符串
- C语言函数详解
- C语言指针详解
- 结构体和联合体
- C语言高级特性和内存管理
- c语言文件操作
- C 语言标准库
- C 语言标准库(二)
- C 语言标准库(三)
- C 语言标准库(四)
- C 语言标准库(五)
C 语言标准库(五)
class fcntl,sys/types,sys/stat当然,除了之前提到的库,还有一些不太常见但仍然有用的标准库和功能。以下是一些其他的库和功能:
1. <fcntl.h> —— 文件控制库
<fcntl.h> 提供了对文件描述符的控制功能,包括打开文件、设置文件状态等。
常用函数
int open(const char *pathname, int flags):打开文件并返回文件描述符。int fcntl(int fd, int cmd, ...):操作文件描述符,通常用于设置文件状态标志。
示例:打开文件
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main() {
int fd = open("example.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("打开文件失败");
return 1;
}
close(fd);
return 0;
}
2. <sys/types.h> 和 <sys/stat.h> —— 系统类型与状态库
这些库提供了对文件和系统状态的基本定义和操作。
常用结构和宏
struct stat:存储文件状态信息。int stat(const char *pathname, struct stat *statbuf):获取文件状态。
示例:获取文件状态
#include <stdio.h>
#include <sys/stat.h>
int main() {
struct stat fileStat;
if (stat("example.txt", &fileStat) == 0) {
printf("文件大小: %ld 字节\n", fileStat.st_size);
} else {
perror("获取文件状态失败");
}
return 0;
}
3. <unistd.h> —— UNIX 标准库
<unistd.h> 提供了与操作系统交互的底层功能,如文件操作、进程控制和用户环境等。
常用函数
ssize_t read(int fd, void *buf, size_t count):从文件描述符读取数据。ssize_t write(int fd, const void *buf, size_t count):向文件描述符写入数据。
示例:读取和写入文件
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main() {
int fd = open("example.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("打开文件失败");
return 1;
}
const char *text = "Hello, world!";
write(fd, text, 13); // 写入数据
lseek(fd, 0, SEEK_SET); // 移动文件指针到开头
char buffer[14] = {0};
read(fd, buffer, 13); // 读取数据
printf("读取内容: %s\n", buffer);
close(fd);
return 0;
}
4. <string.h> —— 字符串处理库
虽然之前提到过,但可以更深入地探讨其函数。
常用函数
size_t strlen(const char *str):计算字符串长度。char *strcpy(char *dest, const char *src):复制字符串。int strcmp(const char *str1, const char *str2):比较两个字符串。char *strcat(char *dest, const char *src):连接两个字符串。
示例:字符串操作
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2); // 连接字符串
printf("连接结果: %s\n", str1);
printf("字符串长度: %zu\n", strlen(str1));
return 0;
}
5. <stdlib.h> —— 标准库
尽管已经提到过,但其中的其他函数也很有用。
常用函数
void *malloc(size_t size):分配内存。void free(void *ptr):释放内存。int rand(void):生成随机数。void srand(unsigned int seed):设置随机数种子。
示例:内存分配和随机数生成
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(5 * sizeof(int)); // 分配内存
if (arr == NULL) {
perror("内存分配失败");
return 1;
}
srand(42); // 设置随机数种子
for (int i = 0; i < 5; i++) {
arr[i] = rand() % 100; // 生成随机数
}
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr); // 释放内存
return 0;
}
6. <locale.h> —— 本地化支持
提供了处理不同地区和语言环境的功能。
常用函数
char *setlocale(int category, const char *locale):设置当前的区域设置。struct lconv *localeconv(void):获取当前区域设置的信息。
示例:设置区域设置
#include <stdio.h>
#include <locale.h>
int main() {
setlocale(LC_ALL, ""); // 设置为环境变量指定的区域
printf("当前区域设置: %s\n", setlocale(LC_ALL, NULL));
return 0;
}
7. <stdint.h> —— 定长整数类型
提供了跨平台的一致整数类型。
常用类型
int8_t、uint8_t:8 位有符号和无符号整数。int16_t、uint16_t:16 位有符号和无符号整数。
示例:使用定长整数
#include <stdio.h>
#include <stdint.h>
int main() {
int32_t num = -42;
uint32_t unum = 42;
printf("有符号整数: %d\n", num);
printf("无符号整数: %u\n", unum);
return 0;
}
这些库和功能虽然不如前面提到的那么常见,但在特定情况下非常有用。掌握这些库能够进一步增强你在 C 语言编程中的能力。
评论区
评论列表