您的位置:首页 > 运维架构 > Linux

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

2013-01-07 22:00 423 查看
*

**系统调用:创建一个文件

**函数原型: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐