gpt4 book ai didi

c - c 程序中 atexit() 函数的任何小实例

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:36 24 4
gpt4 key购买 nike

我在 Unix 程序中阅读了 atexit() 函数,也知道它是如何工作的,但是我找不到任何可以使用它的情况。任何人都可以提供一个实用的 C 程序小例子吗?

最佳答案

这是一个有点傻的例子。

#include <stdio.h>
#include <stdlib.h>


static char temp_filename[255] = "/tmp/tempXXXXXX";


static void do_work(void)
{
/* create a temporary file */
mkstemp(temp_filename);
printf("Created temporary file %s\n", temp_filename);

/* simulate some work */
sleep(10);
}

static void cleanup(void)
{
/* remove temporary files, etc. */
unlink(temp_filename);

printf("Exiting...\n");
}

int main(int argc, char **argv)
{
/* register cleanup handler */
atexit(cleanup);

printf("Starting...\n");

/* do some work */
do_work();

return 0;
}

关于c - c 程序中 atexit() 函数的任何小实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23102982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com