函数声明 | 函数功能 |
---|---|
| 它是 |
| 用于释放通过对象堆栈分配的所有内存。其中, |
| 用于从对象堆栈中分配指定大小的内存,并返回其地址。其中, |
| 用于向对象堆栈添加指定数量的空间,并返回指向添加的第一个字节的指针。其中, |
| 用于将数据复制到对象堆栈,并返回指向添加的第一个字节的指针。其中, |
| 它是一个宏,用于获取结构体中某个成员的偏移量。 |
| 用于打开文件 |
| 它是 Linux 系统定义的一个函数,它可以打开一个相对于指定目录的文件。与 |
| 它是 POSIX 标准定义的一个函数,用于打开目录并返回一个指向 |
| 它是 POSIX 标准定义的一个函数,用于打开一个伪终端(PTY)并返回与之关联的主从设备文件描述符。伪终端可以用于在进程之间建立通信,或者在程序中模拟终端行为。 |
| 它是 POSIX 标准定义的一个函数,用于在进程退出时调用注册的回调函数。这个函数可以用于在程序异常退出或者正常退出时执行一些清理工作、记录日志等操作 |
| 在图形视区显示一个字符串 |
| 在指定位置显示一字符串 |
函数声明 | 函数功能 |
---|---|
| 它是 |
| 用于释放通过对象堆栈分配的所有内存。其中, |
| 用于从对象堆栈中分配指定大小的内存,并返回其地址。其中, |
| 用于向对象堆栈添加指定数量的空间,并返回指向添加的第一个字节的指针。其中, |
| 用于将数据复制到对象堆栈,并返回指向添加的第一个字节的指针。其中, |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <obstack.h>
int main(void)
{
struct obstack my_obstack;
const char *str1 = "Hello, ";
const char *str2 = "World!";
char *dst;
obstack_init(&my_obstack);
dst = (char *)obstack_alloc(&my_obstack, strlen(str1) + strlen(str2) + 1);
strcpy(dst, str1);
strcat(dst, str2);
printf("%s\n", (char *)my_obstack.chunk);
dst = (char *)obstack_blank(&my_obstack, sizeof(int)*2);
int a = 100;
int b = 200;
memcpy(dst, &a, sizeof(int));
memcpy(dst+sizeof(int), &b, sizeof(int));
printf("%d %d\n", *(int *)(my_obstack.next_free-sizeof(int)*2), *(int *)(my_obstack.next_free-sizeof(int)));
double d = 3.1415926;
dst = (char *)obstack_grow(&my_obstack, &d, sizeof(double));
printf("%f\n", *(double *)(my_obstack.next_free-sizeof(double)));
obstack_free(&my_obstack, NULL);
return 0;
}
在上述的程序中,
my_obstack
的 struct obstack
类型变量,并将其传递给 obstack_init()
函数以初始化对象堆栈。obstack_alloc()
函数从对象堆栈中分配一块内存,并将两个字符串连接起来。obstack_blank()
函数向对象堆栈添加一块指定大小的空间,并使用 memcpy()
函数将两个整数复制到该空间中。obstack_grow()
函数向对象堆栈添加一个双精度浮点数,并返回指向该浮点数的指针。printf()
函数将连接后的字符串、添加的整数和添加的双精度浮点数输出到终端,并使用 obstack_free()
函数释放通过对象堆栈分配的所有内存。注意:在使用
obstack_blank()
函数向对象堆栈添加空间时,建议使用sizeof
运算符来计算要添加的空间大小,并在使用memcpy()
复制数据时也应该小心不要越界。同时,在使用obstack_grow()
函数向对象堆栈添加数据时,需要小心指针是否正确,并且操作前需要先使用memcpy()
将要添加的数据复制到一个临时变量中。
宏定义 | 宏描述 |
---|---|
| 它是一个宏,用于获取结构体中某个成员的偏移量。 |
参数:
返回值: 一个 size_t 类型的值,表示该成员变量在结构体中的偏移量(单位是字节)。
#include <stdio.h>
#include <stddef.h>
struct example {
int a;
char b;
double c;
};
int main(void)
{
size_t offset_b = offsetof(struct example, b);
printf("Offset of 'b' in struct example: %zu\n", offset_b);
return 0;
}
在这个程序中,
example
的结构体类型,并使用 offsetof
宏获取结构体中成员变量 b
的偏移量。printf()
函数将结果输出到终端。注意: 在使用
offsetof
宏时,结构体类型名称必须使用括号括起来,否则代码会产生语法错误。此外,offsetof
宏的参数必须是已定义的结构体类型名称和该结构体类型中的成员变量名称,否则也会导致编译错误。
函数声明 | 函数功能 |
---|---|
| 用于打开文件 |
参数:
O_RDONLY
:只读模式打开文件。O_WRONLY
:只写模式打开文件。O_RDWR
:读写模式打开文件。O_CREAT
:如果文件不存在,则创建它。O_TRUNC
:如果文件已存在,则将其长度截断为 0。O_APPEND
:在文件末尾追加数据。O_CREAT
选项,则必须提供这个参数#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(void)
{
int fd = open("temp.txt", O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
}
char buf[1024];
ssize_t nread;
while ((nread = read(fd, buf, sizeof(buf))) > 0) {
if (write(STDOUT_FILENO, buf, nread) != nread) {
perror("write");
exit(1);
}
}
if (nread == -1) {
perror("read");
exit(1);
}
if (close(fd) == -1) {
perror("close");
exit(1);
}
return 0;
}
在上述的程序中,
open()
函数打开文件 temp.txt
,并通过 read()
函数读取其中的数据。write()
函数将数据写入标准输出,直到读取完整个文件。close()
函数关闭文件。注意: 在使用
open()
函数打开文件时,返回值为负数则表示出现了错误。此时可以使用perror()
函数输出错误信息,并使用exit()
函数退出程序。同时,在使用read()
函数和write()
函数读写文件时也需要小心处理返回值,以避免出现不可预期的错误。
函数声明 | 函数功能 |
---|---|
| 它是 Linux 系统定义的一个函数,它可以打开一个相对于指定目录的文件。与 |
参数:
AT_FDCWD
,则表示使用当前工作目录。O_RDONLY
:只读模式打开文件。O_WRONLY
:只写模式打开文件。O_RDWR
:读写模式打开文件。O_CREAT
:如果文件不存在,则创建它。O_TRUNC
:如果文件已存在,则将其长度截断为 0。O_APPEND
:在文件末尾追加数据。O_DIRECTORY
:要求打开的文件必须是一个目录。O_NOFOLLOW
:不跟随符号链接打开文件。O_CREAT
选项,则必须提供这个参数#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
int main(void)
{
int dirfd = open(".", O_RDONLY | O_DIRECTORY);
if (dirfd == -1) {
perror("open");
exit(1);
}
DIR *dirp = fdopendir(dirfd);
if (dirp == NULL) {
perror("fdopendir");
exit(1);
}
struct dirent *entry;
while ((entry = readdir(dirp)) != NULL) {
printf("%s\n", entry->d_name);
}
if (closedir(dirp) == -1) {
perror("closedir");
exit(1);
}
return 0;
}
在这个程序中,
openat()
函数打开当前目录,并通过 fdopendir()
函数将文件描述符转换为目录流。readdir()
函数读取目录中的文件,并将文件名输出到终端。closedir()
函数关闭目录。注意: 在使用
openat()
函数打开文件时,可以通过传递不同的文件描述符指定要打开的目录,从而实现更加灵活的操作。此外,在使用readdir()
函数读取目录时也需要注意判断返回值是否为NULL
,以避免出现不可预期的错误。
函数声明 | 函数功能 |
---|---|
| 它是 POSIX 标准定义的一个函数,用于打开目录并返回一个指向 |
参数:
返回值:
DIR
类型的指针;NULL
。#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
int main(void)
{
DIR *dirp = opendir(".");
if (dirp == NULL) {
perror("opendir");
exit(1);
}
struct dirent *entry;
while ((entry = readdir(dirp)) != NULL) {
printf("%s\n", entry->d_name);
}
if (closedir(dirp) == -1) {
perror("closedir");
exit(1);
}
return 0;
}
在上述的程序中,我们使用 opendir()
函数打开当前目录,并通过 readdir()
函数读取目录中的文件名,最后使用 closedir()
函数关闭目录。
注意: 在使用
opendir()
函数打开目录时,返回值为NULL
则表示出现了错误。此时可以使用perror()
函数输出错误信息,并使用exit()
函数退出程序。同时,在使用readdir()
函数读取目录时也需要小心处理返回值,以避免出现不可预期的错误。
函数声明 | 函数功能 |
---|---|
| 它是 POSIX 标准定义的一个函数,用于打开一个伪终端(PTY)并返回与之关联的主从设备文件描述符。伪终端可以用于在进程之间建立通信,或者在程序中模拟终端行为。 |
参数:
NULL
NULL
NULL
返回值:
0
;-1
。#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(void)
{
int master, slave;
char buf[1024];
ssize_t nread;
if (openpty(&master, &slave, NULL, NULL, NULL) == -1) {
perror("openpty");
exit(1);
}
printf("Slave device: /dev/pts/%d\n", slave);
while ((nread = read(STDIN_FILENO, buf, sizeof(buf))) > 0) {
if (write(master, buf, nread) != nread) {
perror("write");
exit(1);
}
}
if (nread == -1) {
perror("read");
exit(1);
}
if (close(master) == -1) {
perror("close");
exit(1);
}
return 0;
}
在上述的程序中,
openpty()
函数打开一个伪终端,并通过 read()
函数读取标准输入中的数据。注意: 在使用
openpty()
函数打开伪终端时,返回值为-1
则表示出现了错误。此时可以使用perror()
函数输出错误信息,并使用exit()
函数退出程序。同时,在使用read()
函数和write()
函数读写文件时也需要小心处理返回值,以避免出现不可预期的错误。
函数声明 | 函数功能 |
---|---|
| 它是 POSIX 标准定义的一个函数,用于在进程退出时调用注册的回调函数。这个函数可以用于在程序异常退出或者正常退出时执行一些清理工作、记录日志等操作 |
参数:
返回值:
0
;-1
。#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void cleanup(int status, void *arg)
{
printf("Cleanup function called with status %d\n", status);
}
int main(void)
{
if (on_exit(cleanup, NULL) != 0) {
perror("on_exit");
exit(EXIT_FAILURE);
}
printf("This is the main program\n");
return 0;
}
在如上的程序中,
on_exit()
函数注册了一个回调函数 cleanup()
,并将其参数设置为 NULL。函数声明 | 函数功能 |
---|---|
| 在图形视区显示一个字符串 |
参数:
char *textstring
:指向以空字符('\0')结尾的字符串的指针。#include <graphics.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
initgraph(&gdriver, &gmode, "");
midx = getmaxx() / 2;
midy = getmaxy() / 2;
moveto(midx, midy);
outtext("This ");
outtext("is ");
outtext("a ");
outtext("test.");
getch();
closegraph();
return 0;
}
在上述的程序中,
initgraph()
函数初始化图形系统;moveto()
函数将当前绘图位置移动到屏幕中心。outtext()
函数输出一段文字,然后等待用户按下任意键,并关闭图形窗口。函数声明 | 函数功能 |
---|---|
| 在指定位置显示一字符串 |
参数:
int x
: 字符串输出的水平起始坐标(单位为像素)。取值范围:与当前图形模式的屏幕分辨率相关(例如,640x480 模式下,x 范围为 0 到 639)。int y
: 字符串输出的垂直起始坐标(单位为像素)。取值范围:与当前图形模式的屏幕分辨率相关(例如,480p 模式下,y 范围为 0 到 479)。char *textstring
: 指向以空字符('\0')结尾的字符串的指针。#include <graphics.h>
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int x, y;
initgraph(&gdriver, &gmode, "");
x = getmaxx() / 2;
y = getmaxy() / 2;
outtextxy(x, y, "Hello, world!");
getch();
closegraph();
return 0;
}
在上述这个程序中,
initgraph()
函数初始化图形系统并创建一个窗口;(x, y)
并使用 outtextxy()
函数在该位置输出一段文本。getch
函数等待用户按下任意键,然后关闭图形窗口。原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有