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
**系统调用:创建一个文件
**函数原型: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
相关文章推荐
- Linux应用程序-文件编程-file_open()函数疑问
- Linux应用程序开发笔记->文件编程之系统调用方式相关函数
- Linux应用程序开发笔记->文件编程之库函数
- linux应用程序设计(一)——文件、时间编程及进程控制
- Linux执行.sh文件,提示No such file or directory的问题的解决方法
- is_uploaded_file函数引发的不能上传文件问题
- Linux下应用程序的文件操作函数总结
- 关于finfo_file函数获取文件mime值验证出错的问题
- 关于File.separator 文件路径:window与linux下路径问题(“No such file or diretory ”异常解决方案)
- 关于File.separator 文件路径:wind与linux下路径问题 .
- python核心编程学习笔记-2016-08-02-01-读取文件的函数中的文件指针问题
- Linux执行.sh文件,提示No such file or directory的问题的解决方法
- linux终端出现bash: setup.bash: No such file or directory,和.bashrc文件的问题
- linux下编程的gcc编译中关于头文件与库文件搜索路径相关问题(一)
- 【Linux编程】C/C++获取目录下文件或目录及linux中fork()函数详解(原创!!实例讲解)
- Linux执行.sh文件,提示No such file or directory的问题的解决方法
- 关于File.separator 文件路径:wind与linux下路径问题 .
- 函数模型Linux系统文件I/O编程(三)---I/O多路复用
- Linux文件编程之--库函数pk系统调用函数
- Linux执行.sh文件,提示No such file or directory的问题的解决方法