linux之creat函数解析

本文介绍如何使用creat函数替代open函数创建文件,并通过检查返回值判断文件创建是否成功。通过简单的代码示例展示文件操作的基本流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

creat函数完全可以被open函数替代:
open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);

[lingyun@localhost create]$ vim creat.c

 + creat.c                                                                                                     
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  creat.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/28/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "07/28/2013 01:05:30 PM"
 *                 
 ********************************************************************************/


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


#define PERM 0755


int main(void)
{
    static char filename[] = "file.txt";
    int fd;


    fd = creat(filename,PERM);


    if(fd < 0)
        printf("[%s] create fail !!!\n",filename);
    else
        printf("[%s] open success !\n",filename);
    exit(0);
}
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
~                                                                                                               
 ~/apue/creat/creat.c[+]   CWD: /usr/local/src/lingyun/apue/creat   Line: 17/33:18                           
"create.c" 33L, 869C written


[lingyun@localhost creat]$ gcc creat.c 
[lingyun@localhost creat]$ ./a.out 
[file.txt] open success !
[lingyun@localhost creat]$ ls
a.out  creat.c  file.txt



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fulinux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值