Linux应用程序-文件编程-file_creat()函数的问题

本文详细介绍了在编程中使用系统调用`creat`来创建文件的过程,包括其函数原型、参数解释以及实例演示。

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

*
**系统调用:创建一个文件
**函数原型:int creat(const char *filename,mode_t mode);
**参数:filename->要创建的文件名(包含路径,缺省为当前路径)
           mode->创建模式
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void create_file(char *filename)
{
        if(creat(filename,0777)<0)
        {
                printf("create file %s failure!\n",filename);
        }
        else
        {
                printf("create file %s success!\n",filename);
        }
}
int main(int argc,char *argv[])              //argc:记录有多少个参数;argv[]:参数保存在字符串数组里面。
{
        int i;
        if(argc<2)           //这里参数为咋:argc<2时,说明没有输入文件名呢,输入文件名以后,文件名到底是那些参数呢?这些参数怎样传给
        {                         //main函数中的argc呢??这真邪门,暂时还不懂什么来由!!
                perror("you haven't input filename,please try again!\n");
                exit(EXIT_FAILURE);
        }
      for(i=1;i<argc;i++)
      {
               create_file(argv[i]);
      }
               exit(EXIT_SUCCESS);
}
实验如下:
[root@localhost File]# vi file_creat.c 
[root@localhost File]# ls
file_creat.c  file_open  file_open.c  hello
[root@localhost File]# gcc file_creat.c -o file_creat
[root@localhost File]# ls
file_creat  file_creat.c  file_open  file_open.c  hello
[root@localhost File]# ./file_creat 
you haven't input filename,please try again!
: Success
[root@localhost File]# ./file_creat helloworld
create file helloworld success!
[root@localhost File]# ls
file_creat  file_creat.c  file_open  file_open.c  hello  helloworld
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值